Zolbayar Orshil
Zolbayar Orshil

Reputation: 9

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();

Upvotes: 0

Views: 40

Answers (2)

LoneCodeRanger
LoneCodeRanger

Reputation: 511

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.

Upvotes: 0

Nathan
Nathan

Reputation: 499

Change from !== to !=. You're doing a strict equality check on 10 vs '10'.

or !== '10'

Upvotes: 1

Related Questions