(我已经知道这不是 99 瓶代码挑战的最优雅解决方案,但我真的很想知道如何在未来不重蹈覆辙。)
当它在控制台中运行时,它会重复 (count === 0)
条件并且只重复 "0 bottles of beer"
控制台日志,直到它崩溃.
我曾尝试在计数减至 0 后使用“break”语句,但没有取得任何成功。
let count = 99;
function bottlesOfBeer() {
while (count >= 0) {
if (count > 0) {
console.log(count + " bottles of beer on the wall, " + count + " bottles of beer,");
count--;
console.log(" take one down, pass it around, " + count + " bottles of beer on the wall.");
};
if (count === 0) {
console.log(count + " bottles of beer on the wall, " + count + " bottles of beer. Go to the store, buy some more, 99 bottles of beer on the wall.");
} //*this is where I tried the break statement*
}
};
bottlesOfBeer();
最佳答案
你只在 count
大于 0 时递减它,所以它永远不会低于 0;但只要 count >= 0
,循环就会继续。
关于JavaScript 99Bottles - 无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62648431/