0

I'm logging 'undefined' with the following function:

const names = ["James", "Simon", "Alex"];

function fullName(...argument) {
  argument.map(function(name) {
       return `${name} Surname`
      })
}
      console.log(fullName(names)) //Error
      
      

Thanks in advance

4
  • The code in your question actually gives "SyntaxError: missing } after function body"... Commented Jan 17, 2018 at 9:42
  • because you don't return anything from your function. Besides that, your function expects multiple arguments, not a single argument of type Array Commented Jan 17, 2018 at 9:43
  • great @Andy first thing i caught haha Commented Jan 17, 2018 at 9:43
  • 1
    It's really unclear why you're try to use the rest parameters here, as that just adds the array of names inside a new array. Why not just have the function accept an array, and return the result of the map over that? Commented Jan 17, 2018 at 9:51

2 Answers 2

1

You need to add a return statement to the map as the function returns nothing it returns undefined.

But i still feel a bit septical why are using spread operator and passing it as it will pick only last value of the array if you want to add it to every element in the array you need to pass the array instead of indiviual elements

const names = ["James", "Simon", "Alex"];

function fullName(...argument) {
  return argument.map(function(name) {
       return `${name} Surname`
      })
      }
      console.log(fullName(names));
      
      


I feel it should be this you call though

const names = ["James", "Simon", "Alex"];

function fullName(names) {
  return names.map(function(name) {
       return `${name} Surname`
      })
      }
      console.log(fullName(names));

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

Comments

0

So, as I mentioned in the comments, it's not clear what you're trying to achieve here. First, that's not the spread operator. The ... notation when used in a function parameter list are for the rest parameters.

The rest parameter syntax allows us to represent an indefinite number of arguments as an array.

What this means for your example code is that when you pass in names and then use rest parameters your array of names is being placed inside a new array which means that your map would only iterate over the first element (an array) to produce:

[
  "James,Simon,Alex Surname"
]

...which is wrong.

Really all you need to do is pass in the array, have the function accept an array as an argument, map over the names and return the new array.

const names = ["James", "Simon", "Alex"];

function fullName(arr) {
  return arr.map(name => `${name} Surname`);
}

console.log(fullName(names));

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.