Trim Leading and Trailing Spaces

Trim leading and trailing spaces as undefined.

trimName takes a person as an argument. Man will always be an object. Return undefined if the name is not defined. Otherwise, return the truncated 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 name is defined. if (person.name = 'undefined') { return 'undefined'; } else { trimmedName = person.name.trim(); return trimmedName; } } trimName(' sam '); 
+4
source share
3 answers

Ï I don’t see the question there ... but I see problems with the code:

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

the 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/

+3
source

You have to 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 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

+2
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’s object, set his name as the leading and final space. We pass it to a function where we test if it is defined. If so, we return the name cropped.

Further 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 
0
source

Source: https://habr.com/ru/post/1395737/


All Articles