I tried to return the Spread syntax directly but why this code doesn't works?

Tell us what’s happening:

Your code so far

function titleCase(str) {
return ...str.toLowerCase().split(" ").map(wrd => wrd[0].toUpperCase() + wrd.slice(1));

titleCase("I'm a little tea pot");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Edg/88.0.705.50.

Challenge: Title Case a Sentence

Link to the challenge:

Hello @alirezae973.
Welcome to FCC.

You use the spread syntax in an array like [...array]. Remove it and convert the array back to a string using .join(" ") at the end. So that you have something like:

str.toLowerCase().split(" ").map(wrd => wrd[0].toUpperCase() + wrd.slice(1)).join(" ");
1 Like

Hello @nibble and thanks you for your help,
I have found about .join(" ") method but I was wondering why I can’t use spread syntax like this, while using :

 console.log(...str.toLowerCase().split(" ").map(wrd => wrd[0].toUpperCase() + wrd.slice(1))); 

prints the string I'm A Little Tea Pot pretty well ?!

You can do something like:

const arr = [1, 2, 3, 4, 5];
console.log(...arr);  // same as  console.log(1, 2, 3, 4, 5)

with console.log because it is a function taking the elements of the array as arguments. But outside an array or a function invocation, you can’t use the ... syntax. Read more at MDN.

1 Like