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);
2
  • Does this answer your question? How to get numeric value from a prompt box? Commented Apr 9, 2021 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, it will never be equal. Commented Apr 9, 2021 at 11:26

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

7 Comments

Hi! Thanks for your help. But it still doesn't work the way I expected.
So, what's the desired results? With the code above, it keeps asking you to guess a number, until you find the right one
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".
It breaks the loop at the first input
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 !
|
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.

2 Comments

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".
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 !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.