javascript - 使用 while 循环遍历数组
问题描述
我正在通过训练营实验室并在循环实验室中面临一些挑战。该练习要求创建一个接受一个参数的函数 - 一组事实。我必须使用 while 循环来遍历事实数组,添加!!!
到每个事实的末尾,并返回带有感叹号的字符串数组。
const facts = [
"He was the last Beatle to learn to drive",
"He was never a vegetarian",
"He was a choir boy and boy scout",
"He hated the sound of his own voice"
];
function johnLennonFacts() {
let i = 0;
while (i <= facts.lenght) {
i++;
console.log(facts[i] + '!!!');
}
return facts
}
解决方案
这是您想要实现的目标:
const facts = [
"He was the last Beatle to learn to drive",
"He was never a vegetarian",
"He was a choir boy and boy scout",
"He hated the sound of his own voice"
];
// With one argument (array of facts) it's better :p
function johnLennonFacts(facts) {
let i = 0;
const factsModified = [];
while (i <= facts.length) {
factsModified.push(facts[i] + '!!');
i++;
}
return factsModified;
}
推荐阅读
- multithreading - 创建一个将阻塞的函数,直到它被超过 n/2 个线程调用(伪代码)
- javascript - 如何使用 JohMun/vue-tags-input 发布标签值
- superpower - SuperPower 解析器运行时异常:值不能为空。参数名称:解析器
- html - 如何换行长单词并保持正常单词不换行
- python - 如何过滤包含 F() 的 Sum()?
- qt - QML - 为什么 TabBar 的implicitWidth 依赖于将内部 TabButton 的宽度绑定到它的implicitWidth?
- node.js - Cloudconvert - 使用请求承诺通过 API 上传时出现无效签名错误
- scala - 通过多个对象传递隐式参数
- javascript - 通过将布尔值转换为数组来减小 JSON 大小是个好主意吗?
- c - 每当我输入 -1 时,什么都没有发生