首页 > 解决方案 > 谁能告诉我为什么这个循环永远运行?

问题描述

let dice = Math.floor(Math.random() * 6) + 1;
while (dice !== 6) {
    console.log(You rolled a ${dice}); 
}

请解释一下这段代码?

标签: javascriptloops

解决方案


您只需要在循环中分配dice值,直到whiledice !== 6

let dice = Math.floor(Math.random() * 6) + 1;
while (dice !== 6) {
  console.log(`You rolled a ${dice}`);
  dice = Math.floor(Math.random() * 6) + 1;
}


推荐阅读