smailwail
smailwail

Reputation: 1

Javascript (break)

Im in the process of learning javascript and I cant seem to understand why the break statement isnt functioning :( Can someone please tell me what I've done wrong?

let maximum = parseInt(prompt("Enter the maximum number")); 
while(!maximum){ 
    maximum = parseInt(prompt("Enter a valid number")); 
}
const randomNum = Math.floor(Math.random() * maximum) + 1; 
console.log(randomNum);
let guess = parseInt(prompt(`Enter your guess for the generated number between 1 and the maximum number of ${maximum}.`));
let attempts = 1;

while (parseInt(guess) !== randomNum){
    if (guess === 'q') break;
    attempts++;
    if(guess > randomNum){
        guess = prompt("Too high, guess again.");
    } else{
        guess = prompt("Too low, guess again.");
    }
}
if (guess === 'q'){
    console.log("Quitting.")
} else {
console.log(`It took you ${attempts} amounts of guesses!`)
}

Upvotes: 0

Views: 90

Answers (3)

SirOneOfMany
SirOneOfMany

Reputation: 1084

if (guess === 'q')

You are parsing the value to an integer and are comparing it to a string. Which is always false

So when you type 'q' in your prompt and try parseInt on it you will get NaN which stands for not a number. And NaN is not equal with q obviously

EDIT:

as @axic correctly pointed out the condition from above cannot be fulfilled if q was typed before the iteration begins. But that brings another problem: On the third iteration you will get another prompt saying "Too low, guess again." even if you guessed the right number, because guess is string and compared to a number which will return false in all cases.

Upvotes: 3

SirShine
SirShine

Reputation: 1

The break statement works totally fine, the issue is in a different location of your logic. You are parsing the integer of guess when displaying the first prompt to enter a guess for the number but when entering anything that isn't a number the value will simply be NaN.

Line 7 is:

let guess = parseInt(prompt(`Enter your guess for the generated number between 1 and the maximum number of ${maximum}.`));

but should be:

let guess = prompt(`Enter your guess for the generated number between 1 and the maximum number of ${maximum}.`);

I'm not entirely sure if this is what you want to achieve but based on the little information you gave I assumed that this would be what you're trying to get.

Upvotes: 0

Ferin Patel
Ferin Patel

Reputation: 3978

You are parsing the value to an integer and are comparing it to a string.

let maximum = parseInt(prompt("Enter the maximum number"));
while (!maximum) {
  maximum = parseInt(prompt("Enter a valid number"));
}
const randomNum = Math.floor(Math.random() * maximum) + 1;
console.log(randomNum);
let guess = prompt(`Enter your guess for the generated number between 1 and the maximum number of ${maximum}.`);
let attempts = 1;

while (parseInt(guess) !== randomNum) {
  if (guess === 'q') break;
  attempts++;
  if (guess > randomNum) {
    guess = prompt("Too high, guess again.");
  } else {
    guess = prompt("Too low, guess again.");
  }
}
if (guess === 'q') {
  console.log("Quitting.")
} else {
  console.log(`It took you ${attempts} amounts of guesses!`)
}

Upvotes: 1

Related Questions