【发布时间】:2022-01-15 05:15:16
【问题描述】:
/*Your task in this activity is to implement a function called createParagraph that takes an array of strings, and returns a single string containing all the items in the array separated by a space.*/
/*Your task in this activity is to implement a function called createParagraph that takes an array of strings, and returns a single string containing all the items in the array separated by a space.*/
//Use forEach to create a single paragraph from a list of words.
// This is a list of words
let words = ['Loops', 'are', 'a', 'great', 'way', 'to', 'find', 'elements', 'in', 'an', 'array'];
// TODO: implement this function to return a string containing all words in a paragraph.
function createParagraph(words) {
let paragraph = '';
return paragraph.forEach(words);
}
// Prints paragraph to console
console.log(createParagraph(words));
// don't change this line
if (typeof module !== 'undefined') {
module.exports = createParagraph;
}
【问题讨论】:
-
直接返回
return words.join(' '); -
本课是否要求您使用 forEach?如果是这样,那么它是 words.forEach,并且该块将其参数附加到段落
标签: javascript arrays string if-statement foreach