首页 > 解决方案 > JavaScript 中的 while 循环递减,直到达到 0 并打印“Blastoff”

问题描述

锻炼:

声明一个名为 countdown 的变量并将其赋值为 10。在while循环中,每次迭代将 countdown 的值递减一次并打印出来。一旦倒计时达到 0,将“Blastoff”打印到控制台。

到目前为止我所做的:

var countdown = 10;{
    while (countdown > 0 || 0=== "Blastoff!"){
        console.log(countdown);
        countdown = countdown - 1;
    }
    console.log("Blastoff!");
}

到目前为止,这是我认为应该是正确的结果:

Output
>>>>Code is incorrect
The first line in your while's block should decrement the value of the variable countdown
10
9
8
7
6
5
4
3
2
1
Blastoff!

标签: javascriptwhile-loopcountdownconsole.logdecrement

解决方案


终于正确了:

var countdown = 10;
while (countdown > 0){
    countdown--;
    console.log(countdown);
}
console.log("Blastoff");


推荐阅读