0

hello to every one i hope you have a good day :)

i need to define a function, It should accept an array of strings which may contain additional space characters.

the function should use the array map method in order to make a new array full of trimmed names.

i tried something like that but its not close to work unfortunately

function cleanNames(array) {
   const trimArray = array.map() function() {
     return array.trim();
   }
}
1

5 Answers 5

2

Your code throws the following syntax error:

Uncaught SyntaxError: Unexpected token 'function'

in array.map() function() {

You have to trim the names inside the map callback function. You can also use arrow function (=>) syntax to shorten the code:

function cleanNames(arr) {
  return arr.map(i => i.trim());
}

console.log(cleanNames(['John ', ' Jane', ' Joe ']));

Sign up to request clarification or add additional context in comments.

Comments

1

You should do it like this:

var array = [" Mike", " Hellen", "Robert "];
array = array.map(function (el) {
  return el.trim();
});
console.log(array);

you have to do the edit in map function.

Comments

1
console.log([" Mike", " Hellen", "Robert "].map(name => name.trim()));

Comments

1

 function cleanNames(input) {
   return input.map(val => val.trim());
}
const names = [" Joy ", " James", "Raj"];
console.log(cleanNames(names));

Comments

0

You should try and make the minimum amount of code:

[' aa ', ' b b ', '   c c '].map(i=>i.trim());

This will solve your problem.

By the way, if you are using jQuery the syntax is a bit different and even simpler:

$.map([' aa ', ' bb ', '   cc '], $.trim); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.