javascript - 这段代码中的while循环有什么作用?
问题描述
这段代码中的while循环有什么作用?
while 循环是如何工作的?
let numApples = Math.random() * 10;
while (numApples > 0) {
console.log("Giving away an apple");
numApples = numApples - 1;
}
console.log("No apples remaining!");
解决方案
基本上在伪代码中它执行以下操作。
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!"
推荐阅读
- angular - 从 Angular 7 迁移到 12 后出现混合内容错误
- amazon-web-services - AWS 管理员没有 EC2 描述操作
- javascript - 如何在 React JS 中的不同行中拆分不同列中的值
- mongodb - MongoDB服务器不是从vs代码终端开始的
- reactjs - 如何从可编辑 div 中的超链接单词中获取链接和文本?
- javascript - React JS:上传多个文件
- sql - SQL将多行转换为多列
- matlab - 配置 S-Function 的输出
- html - 引导网站中的滑块图像不适合移动视图
- ios - Swift - 使用 init 初始化模型对象(来自解码器:)