此代码中的while循环有什么作用?

时间:2018-11-06 05:06:14

标签: javascript

这段代码中的while循环有什么作用?

while循环如何工作?

let numApples = Math.random() * 10;
    
while (numApples > 0) {
    
    console.log("Giving away an apple");
    
    numApples = numApples - 1;
    
}
    
console.log("No apples remaining!");

1 个答案:

答案 0 :(得分:0)

基本上以伪代码执行以下操作。

initiate variable called numApples with a random number 0 - 10.
Then while that number is greater than zero, 
  Output a message to the console saying "Giving away an apple" then
  Subtract one from the numApples number.

Finally when the numApples becomes equal to zero 
  Output a message to the console saying "No apples remaining!"