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
Top comments (0)