Trim function

JavaScript related questions should go here.
Post Reply
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Trim function

Post by EcazS »

Alright, I am no good with JavaScript.
In fact, I am horrible with it :roll:

But I found the Codecademy site from a post here and I've been going on with that since last night and I've finished everything pretty easily except this one thing that I'm stuck with and they don't have a support forum or anything like that (for some reason :S).

I don't really understand what I'm supposed to be doing "code-wise". If anyone could help me that would be awesome :lol:
This is the "description" of the task.
If you try to access a property or method of the value undefined, you'll have an error.

This happens surprisingly often: for instance, person.name was not defined in the previous example. If you tried to do person.nmae.trim(), an error would be thrown.

trim is a function that removes trailing and leading whitespace from a string. For instance " foo " becomes "foo".

Try to implement a common pattern to deal with this issue by completing the trimName function according to the instructions in the comments.

And this is the start-code given.
[syntax=javascript]// trimName accepts a person as an argument. Person
// will always be an object.
// Return undefined if name is not defined. Otherwise,
// return a trimmed name.
function trimName(person) {
// If you do not set this variable to a value, it will be
// undefined.
var trimmedName;
// Do a check here to see if a person's name is defined.
if ( ) {
trimmedName = person.name.trim();
}

return trimmedName;
}[/syntax]

Again, I'm stuck because I don't understand the "description" :lol:
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Trim function

Post by jacek »

It looks like it is asking you to come up with a way to check if a given variable (or anything) is defined.

You can do that like this.

[syntax=javascript]if (typeof var_name !== 'undefined'){
// in here we know that var_name has a value.
}[/syntax]
Image
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Trim function

Post by EcazS »

[syntax=javascript]
function trimName(value) {
var person = {};
person.name = value;
var trimmedName;
// Do a check here to see if a person's name is defined.
if (typeof value !== 'undefined') {
trimmedName = person.name.trim();
}else {
trimmedName = 'undefined';
}

return trimmedName;
}
trimName(" foo ");
[/syntax]
I have it like this but it doesn't seem to be the right way of doing it since it's just saying "Oops try again".......
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Trim function

Post by jacek »

EcazS wrote:I have it like this but it doesn't seem to be the right way of doing it since it's just saying "Oops try again".......

That could just be the site failing to recognise the code properly.

What you have seems right to me.

Try without the else part.

[syntax=javascript]function trimName(value) {
var person = {};
person.name = value;
var trimmedName;

// Do a check here to see if a person's name is defined.
if (typeof value !== 'undefined') {
trimmedName = person.name.trim();
}

return trimmedName;
}[/syntax]
Image
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Trim function

Post by EcazS »

"Ooops, try again"....

Maybe the lesson is broken like some of the other :/ I'll try and contact the course creator and the owners of Codecademy.
c0wb0yz
Posts: 2
Joined: Thu Jan 05, 2012 6:51 pm

Re: Trim function

Post by c0wb0yz »

Hi, have you figured out the solution ?
I'm so stuck. It drives me crazy !
Thanks for your help
c0wb0yz
Posts: 2
Joined: Thu Jan 05, 2012 6:51 pm

Re: Trim function

Post by c0wb0yz »

Actually, a friend of mine helped me out :

[syntax=javascript]function trimName(person) {

var trimmedName;
if (person.name !== undefined) {
trimmedName = person.name.trim();
}

return trimmedName;
}[/syntax]
Last edited by jacek on Sun Jan 08, 2012 12:51 am, edited 1 time in total.
Reason: code tags...
Post Reply