简体   繁体   中英

While loop keep looping even though expression is false

My loop is not quitting when i enter 10. Please help me.

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'.

or !== '10'

Maybe these links can help:

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

And here that, for the prompt 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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