Combining an Array into a String Using the join Method

rthefounding

Randy Rivera

Posted on July 1, 2021

Combining an Array into a String Using the join Method
  • The join method is used to join the elements of an array together to create a string. It takes an argument for the delimiter that is used to separate the array elements in the string.

  • Example:

let arr = ["Playstation", "Rules"];
let str = arr.join(" ");
Enter fullscreen mode Exit fullscreen mode
  • str would have a value of the string Playstation Rules.

  • Let's use the join method inside the word function to make a sentence from the words in the string str. The function should return a string.

function sentence(str) {
  // Only change code below this line


  // Only change code above this line
}
sentence("This-is-where-the-fun-begins");
Enter fullscreen mode Exit fullscreen mode
  • Answer:
function sentence(str) {
return str.split(/\W/).join(" ")

}
console.log(sentence("This-is-where-the-fun-begins")); will return This is where the fun begins
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
rthefounding
Randy Rivera

Posted on July 1, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related