Functional coding phrasing

Tell us what’s happening:
The solution looks a lot like what I wrote but mine doesn’t work. probably because its in 2 lines? can someone help me with what the rule is on this?

Your code so far


function sentensify(str) {
// Only change code below this line
str.split(/\W/);
return str.join(" ");
// Only change code above this line
}
sentensify("May-the-force-be-with-you");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.

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

Link to the challenge:

Strings are not immutable, so try setting str.split to a variable, and go from there.

1 Like

first, split returns a new array, you are not capturing that returned array

second, join is an array method, using it on a string will throw an error


also, strings are immutable - you can’t mutate strings @GhostRoboXt

1 Like

OKAY right. That’s interesting because there are some functions that do seem to mutate them like splice versus slice. I thought about setting up a new variable.

str = str.split(/\W/);
return str.join(" "); // works!!

Is is a bad idea to use an argument like this?

2 Likes

For the smaller codes I don’t think it is bad. But understand that when you want to use str again after the split, it will be splitted.

I think you’re thinking of arrays – strings have a slice method (which returns a new string), no splice method.