简体   繁体   中英

Trim Leading and Trailing Spaces

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:

  • You are using the assignment operator = where you should use the comparison 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.net/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.com/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 comments. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM