简体   繁体   English

即使表达式为假,while循环也会继续循环

[英]While loop keep looping even though expression is false

My loop is not quitting when i enter 10. Please help me.当我输入 10 时,我的循环没有退出。请帮助我。

let getGuessess = function(){
     let guessedNum = null;
     while(guessedNum !== 10){
       guessedNum = prompt(`enter number $`);
      if(guessedNum === "quit"){
      break;
     }
   }
  }
  getGuessess();

Change from !== to != .!==更改为!= You're doing a strict equality check on 10 vs '10'.您正在对 10 与 '10' 进行严格的相等性检查。

or !== '10'!== '10'

Maybe these links can help:也许这些链接可以提供帮助:

https://www.w3schools.com/js/js_comparisons.asp https://www.w3schools.com/js/js_comparisons.asp

I see there that:我在那里看到:

!== means not equal value or not equal type !== 表示不等值或不等类型

https://www.w3schools.com/jsref/met_win_prompt.asp https://www.w3schools.com/jsref/met_win_prompt.asp

And here that, for the prompt function:在这里,对于提示 function:

Return Value: A String.返回值:一个字符串。

I think that it doesn't work because you are comparing a string and an int, they are different types, so your comparison returns False even if you enter 10.我认为它不起作用,因为您正在比较一个字符串和一个 int,它们是不同的类型,所以即使您输入 10,您的比较也会返回 False。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM