Functional Programming - Combine an Array into a String Using the join Method

THE TASK Use the join method (among others) inside the sentensify function to make a sentence from the words in the string str. The function should return a string.

I dont understand my code isnt working here. Ive checked the solution listed in the hints and I feel that this code is functionally the same…

function sentensify(str) {
  // Only change code below this line
let newArr = str.split(/\W/);
newArr.join(" ");
return newArr;

  // Only change code above this line


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0

Challenge: Functional Programming - Combine an Array into a String Using the join Method

Link to the challenge:

Take a look at the .join() function. Check whether it modifies the array in place or returns something new!

1 Like

The .join() function does not modify the array here. If I remove the join function then I still get the same output.

The join method creates and returns a new string. When a method returns something, you can assign the returned value to a variable. That variable could then be returned.

Why does return str.split(/\W/).join(" ");

return a different output than let newArr = str.split(/\W/); newArr.join(" "); return newArr; ?

Because as you already discovered, the join method does not modify newArr (does not turn it into a string). Instead it returns a new string that you would need to assign to another variable and return the other variable.

Yes, I know! I’m not suggesting you remove .join(), I’m suggesting a careful reading of how it operates on arrays and figuring out how to access what it does for you (without trying to give away the answer).

Just takes a while to remember which ones affect return values vs manipulate-in-place.

1 Like

I figured out that it worked when I assigned it to a new variable and then returned that new variable. But Im still finding this rather confusing. Guess now I know moving forward.

Yup, it’s just one of those weird things you have to remember (or remember to look up in the docs regularly). Some functions modify the array in place, and some just return a copy. I have yet to figure out why some do one or some do the other, so I just always test it or look it up if it isn’t one I’m sure of.