Page 1 of 1

Trim function

Posted: Mon Nov 28, 2011 11:11 pm
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:

Re: Trim function

Posted: Tue Nov 29, 2011 5:49 pm
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]

Re: Trim function

Posted: Tue Nov 29, 2011 6:15 pm
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".......

Re: Trim function

Posted: Wed Nov 30, 2011 5:15 pm
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]

Re: Trim function

Posted: Thu Dec 01, 2011 10:22 am
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.

Re: Trim function

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

Re: Trim function

Posted: Thu Jan 05, 2012 9:41 pm
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]