admin管理员组

文章数量:1384402

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 ');

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 ');
Share Improve this question asked Feb 10, 2012 at 9:22 user1201670user1201670 1153 silver badges6 bronze badges 2
  • Do you want to return undefined or 'undefined' (string)? – kapa Commented Feb 10, 2012 at 9:27
  • 3 Bear in mind that String.prototype.trim is not available in all browsers (notably IE below version 9, I think) so you will want to use a shim (or, as you've tagged jQuery) jQuery.trim. – James Allardice Commented Feb 10, 2012 at 9:27
Add a ment  | 

3 Answers 3

Reset to default 3

Ï don't see a question there... but I see issues with the code:

  • You are using the assignment operator = where you should use the parison operator ==.
  • Comparing a string to the string 'undefined' is not the way to check if an property is undefined.
  • The trim method only exists in the latest version (9) of IE.
  • You are calling the function with a string instead of an object.

Code:

function trimName(person) {
  var trimmed;
  if (typeof person.name == 'undefined') {
    trimmed = 'undefined';
  } else {
    trimmed = person.name.replace(/(^\s+|\s+$)/g, '');
  }
  return trimmed;
}

var trimmedName = trimName({ name: ' sam ' });

Demo: http://jsfiddle/Guffa/vCkSq/

You should do

var personTest = {name: '  sam'};

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 (typeof person.name === 'undefined') {
    return 'undefined';
  }
  else {
    var trimmedName = person.name.trim();
    return trimmedName;
  }
}
alert(trimName(' sam '));
alert(trimName(personTest));

pastebin http://jsbin./oqovog/edit#source

function trimName(person) {
  // Check if the name of the person was defined
  // If not, return undefined
  if (person.name == 'undefined') {
    return 'undefined';
  }
  else {
    // Otherwise trim the name and return it.
    return person.name.replace(/^\s+|\s+$/g, '');
  }
}

// Create a person, set his name to " sam " with the spaces.
var person = {};
person.name = " sam ";

// Pass sam (the person object) to your function
// Then alert() the result.
alert(trimName(person));

Take a look at the code here and read the ments. We create a person object, set his name with the leading and trailing space. We pass it to the function where we test if it is defined. If it is, we return the name trimmed.

Following is edited.

var person = {}; //creates the object "person"
person.name = prompt('Please enter a name'); //defines name as a property
function trimName(person) { //and gives it a value
// If the property "name" is undefined
// return undefined
if (name === undefined) { //returns the code state "undefined"
  return undefined; // if name is undefined
} else if (person.name === '') { //returns a prompt if no name is entered
  return 'Please enter a name';
} else {
// Trim the "name" property, ensure it is a string
return (person.name + '').trim(); //trims leading/trailing spaces
}
}
trimName(person); //defines object person as a variable of function trimName

本文标签: javascriptTrim Leading and Trailing SpacesStack Overflow