1

Hope you're all doing well ! I'm newbie with js and I've written the code below but it doesn't work the way I expected it to do. Need help ! The thing is, I want the loop to continue until "numberToGuess = guessNumber" but unfortunately, it breaks at second loop; even if numberToGuess is not equal to guessNumber. Can Someone explain me how to fix that please ? Thx!

const numberToGuess = Math.round(10*Math.random());
let guessNumber;

while(numberToGuess !== guessNumber){
guessNumber = prompt("Guess the hidden number: ");
if(guessNumber < numberToGuess){
console.log("Too low");
}else if(guessNumber > numberToGuess){
console.log("Too high");
}
}

console.log("Congrats ! You found it !");
console.log("Number to guess = " + numberToGuess + "\nGuessed number = " + guessNumber);
E_net4
  • 27,810
  • 13
  • 101
  • 139
Phoenix
  • 11
  • 2
  • Does this answer your question? [How to get numeric value from a prompt box?](https://stackoverflow.com/questions/17907455/how-to-get-numeric-value-from-a-prompt-box) – Ivar Apr 09 '21 at 11:25
  • The type of `numberToGuess` is a number. The `prompt()` always returns a string. If you compare a string with a number using [strict equals comparison](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_inequality), it will never be equal. – Ivar Apr 09 '21 at 11:26

2 Answers2

1

You have to change the way you compare numberToGuess and guessNumber, by replacing !== with !=

Here's the code tested and working

const numberToGuess = Math.round(10*Math.random());
let guessNumber;

while(numberToGuess != guessNumber){
guessNumber = prompt("Guess the hidden number: ");
if(guessNumber < numberToGuess){
console.log("Too low");
}else if(guessNumber > numberToGuess){
console.log("Too high");
}
}

console.log("Congrats ! You found it !");
console.log("Number to guess = " + numberToGuess + "\nGuessed number = " + guessNumber);
EdwardKirk
  • 425
  • 1
  • 7
  • 16
  • Hi! Thanks for your help. But it still doesn't work the way I expected. – Phoenix Apr 09 '21 at 15:53
  • So, what's the desired results? With the code above, it keeps asking you to guess a number, until you find the right one – EdwardKirk Apr 09 '21 at 15:56
  • Hi! Thanks for your help. But it still doesn't work the way I expected. I'll explain: In a first time, the Math.round..... function will generate a random number and store it in the "numberToGuess" variable. Then, the prompt method will ask for user input (it will ask to give a number of course) and, as long as the given number gathered by the prompt method is not equal to the random number saved in the "numberToGuess" variable, I want the code to continue to loop and ask for another input number until the input number (guessNumber) is equal to the random number saved in the "numberToGuess". – Phoenix Apr 09 '21 at 16:01
  • It breaks the loop at the first input – Phoenix Apr 09 '21 at 16:02
  • In all honesty, I can't seem to reproduce your error... The code snipped provided above, doesen't break on first input, I also saved a [JSFiddle](https://jsfiddle.net/0tf27n8w/) and tried it with multiple browsers, it keeps going until I guess the number... What your `console` says? Do you get any errors there? – EdwardKirk Apr 09 '21 at 16:12
  • You're right! Thanks! I've tested it another way it worked ! – Phoenix Apr 09 '21 at 16:38
  • 1
    I was using jsbin.com for testing the code but it doesn't work correctly there, so I used my browser console dev tool and it worked ! Thanks for your help ! – Phoenix Apr 09 '21 at 16:40
1

As correctly said by Ivar, prompt() always return value as string. If you want to compare it you can do as follows:

const numberToGuess = Math.round(10*Math.random());
let guessNumber;

    while(numberToGuess !== Number(guessNumber)){
    guessNumber = prompt("Guess the hidden number: ");
    if(guessNumber < numberToGuess){
    console.log("Too low");
    }else if(guessNumber > numberToGuess){
    console.log("Too high");
    }
    }
    
    console.log("Congrats ! You found it !");
    console.log("Number to guess = " + numberToGuess + "\nGuessed number = " + guessNumber);

Adding Number function will convert the string format into number format, so the comparison will be of numbers itself.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Hi! Thanks for your help. But it still doesn't work the way I expected. I'll explain: In a first time, the Math.round..... function will generate a random number and store it in the "numberToGuess" variable. Then, the prompt method will ask for user input (it will ask to give a number of course) and, as long as the given number gathered by the prompt method is not equal to the random number saved in the "numberToGuess" variable, I want the code to continue to loop and ask for another input number until the input number (guessNumber) is equal to the random number saved in the "numberToGuess". – Phoenix Apr 09 '21 at 16:02
  • I was using jsbin.com for testing the code but it doesn't work correctly there, so I used my browser console dev tool and it worked ! Thanks for your help ! – Phoenix Apr 09 '21 at 16:40