search and replace ```\n< with ```\n\n< to ensure there's an empty line before closing tags
2.1 KiB
2.1 KiB
id, title, challengeType, videoUrl, localeTitle
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d7b86367417b2b2512b3d | Prevent Infinite Loops with a Valid Terminal Condition | 1 | 使用有效的终端条件防止无限循环 |
Description
loopy()
内的while
循环。不要叫这个功能! function loopy(){程序员的工作是确保最终达到终止条件,该条件告诉程序何时突破循环代码。一个错误是从终端条件向错误方向递增或递减计数器变量。另一个是在循环代码中意外重置计数器或索引变量,而不是递增或递减它。
while(true){
console.log(“Hello,world!”);
}
}
Instructions
myFunc()
函数包含一个无限循环,因为终端条件i != 4
将永远不会计算为false
(并且会中断循环) - i
将每次递增2,然后跳过4,因为i
是奇数启动。固定在终端条件比较运算符因此该循环仅运行i
小于或等于4。 Tests
tests:
- text: 您的代码应该更改<code>for</code>循环的终端条件(中间部分)中的比较运算符。
testString: assert(code.match(/i\s*?<=\s*?4;/g).length == 1);
- text: 您的代码应该在循环的终端条件中修复比较运算符。
testString: assert(!code.match(/i\s*?!=\s*?4;/g));
Challenge Seed
function myFunc() {
for (let i = 1; i != 4; i += 2) {
console.log("Still going!");
}
}
Solution
// solution required
/section>