Trim leading and trailing spaces from name “undefined”.
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.
var person = {};
var trimmedName;
person.name;
function trimName(person) {
// If you do not set this variable to a value, it will be
// undefined.
// Do a check here to see if a person's name is defined.
if (person.name = 'undefined') {
return 'undefined';
}
else {
trimmedName = person.name.trim();
return trimmedName;
}
}
trimName(' sam ');
Ï don’t see a question there… but I see issues with the code:
=where you should use the comparison operator==.'undefined'is not the way to check if an property is undefined.trimmethod only exists in the latest version (9) of IE.Code:
Demo: http://jsfiddle.net/Guffa/vCkSq/