Combining an Array into a String Using the join Method
Randy Rivera
Posted on July 1, 2021
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(" ");
str
would have a value of the stringPlaystation Rules
.Let's use the
join
method inside theword
function to make a sentence from the words in the stringstr
. 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");
- 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
💖 💪 🙅 🚩
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
javascript Functional Programming: Passing Arguments to Avoid External Dependence in a Function
June 21, 2021