Combine an Array into a String Using the join Method

Combine an Array into a String Using the join Method
0

#1

Tell us what’s happening:

Your code so far


function sentensify(str) {
  // Add your code below this line
return  str.split(/W+/);
  // Add your code above this line
}
sentensify("May-the-force-be-with-you");

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/combine-an-array-into-a-string-using-the-join-method


#2

Good!

You got the first step right, the str.split(/W+/); or /W/ will split a string with given regex into an array. so the return of split is an array.

Now you need to join the elements of the array together using join to make the asked result.

First, you should not return an array, so return the split value is not right.

Keep the result of the split with a var, like var arr=str.split(/W/); great.

Now just like the assignment and lesson explained about the join() method, use it and return the joined array.

easy, you can do it. (still got issue, need help? it’s okay, reply so we can help you, but first try it)

keep goin on great work, happy programming


#3

This is my solution:

function sentensify(str) {
  return str.split(/\W/).join(' ');
}
sentensify("May-the-force-be-with-you");

#4

function sentensify(str) {
// Add your code below this line
return str.split(/\W+/).join(" ");

// Add your code above this line
}
sentensify(“May-the-force-be-with-you”);
console.log(sentensify(“May-the-force-be-with-you”));

Could you explain the different between the + in (/\W/) and (/\W+)?
Both codes pass the test.


#5

The + is a quantifier where it matches any string that contains at least, in this instance, one non-word character.

(/\W+/)

#6

I was trying to work a weirder approach where more characters separate the string. Trying to accumulate those weird characters and next use them to replace (not with replace) them. It might work without split and join. Still not efficient or clean.


#7

Hello, first split the string to turn it into an array and then join the array to turn it back into an array.

Solution below:

return str
.split(/\W/)
.join(’ ')

Hope this helps!