简体   繁体   中英

Javascript, about infinite looping prompt and stopping that loop

I'm new to javascript, so that's my back story :) This code keeps crashing my browser, and it won't let me cancel the prompt if I don't cancel on the first attempt, whether I enter a number or not...Anyway, even the game won't work, which is to just to get to a point where the secret number is the person's guessing number. For cancelling the prompt, I even tried if(answer===null||answer===false||answer==="null") break; but it won't work. Can anybody help? Thanks.

Here is my code:

<script>
var secret_number=Math.floor((Math.random() * 10) + 1); 
var answer=prompt("Enter a number you want to try");

while(secret_number!==Number(answer))
{
if(name===null||name===false||name==="null")
break;

if(Number(answer)>secret_number)
prompt("This number is too high!");

else if(Number(answer)<secret_number)
prompt("This number is too low!");

else
prompt("You got it!");
}

</script>

I have a follow-up question. I took into account what people told me and came up with this:

var secret_number=Math.floor((Math.random() * 10) + 1); 
var answer=parseInt(prompt("Enter a number you want to try"));

while(secret_number!==answer)
{
if(answer == null || answer == "" ||isNaN(answer))
break;

if(answer>secret_number)
answer=prompt("This number is too high!");

else if(answer<secret_number)
answer=prompt("This number is too low!");

else
alert("You got it!");
}

My question is, how come my "You got it!" alert keeps going on infinitely? I know I can make it break there, but I want to know why it keeps doing that. It should have that secret_number===answer and break out of the while loop, so obviously it's not doing that, and I don't get why. Thank you again.

You've found out why while loops can be dangerous. The best solution here is to limit the amount of guesses the user has and to do that you need a counter that is incremented inside the loop and that can simply be done with a for loop.

Also, use curly braces to surround your if branches.

Lastly, the best way to convert a string into a number is with parseInt() and parseFloat() . Do the conversion right when you get the string and then you don't have to keep converting it every time you want to use it.

 var secret_number = Math.floor((Math.random() * 10) + 1); alert("For testing only: The secret number is: " + secret_number); var answer = parseInt(prompt("Enter a number you want to try"), 10); const GUESSES = 3; for(var i = 0; i < GUESSES; ++i){ if(!isNaN(answer)){ if(answer > secret_number) { answer = prompt("Try again...This number is too high! (" + (GUESSES - i - 1) + " guesses left.)"); } else if(answer < secret_number) { answer = prompt("Try again...This number is too low! (" + (GUESSES - i - 1) + " guesses left.)"); } else { alert("You got it!"); break; } } else { alert("You didn't enter a number!"); break; } } 

The problem is that the prompt is before the loop. Changing while to do while can fix it:

 var secret_number=Math.floor((Math.random() * 10) + 1); do { var str = prompt("Enter a number you want to try"), answer = Number(str); if (!str || isNaN(answer)) { alert('Bye'); break; } if (answer > secret_number) { alert("This number is too high!"); } else if (answer < secret_number) { alert("This number is too low!"); } else { alert("You got it!"); } } while (secret_number !== answer); 

Try this. Hope it helps.

 <script> var secret_number=Math.floor((Math.random() * 10) + 1); var answer=prompt("Enter a number you want to try"); while(secret_number!=Number(answer)){ if(Number(answer)>secret_number){ answer = prompt("This number is too high!"); } else if(Number(answer)<secret_number){ answer = prompt("This number is too low!"); } } prompt('correct!'); </script> 

This is only different from some of the other answers in trying to retain your intent of giving up when no answer is provided:

var secret_number=Math.floor((Math.random() * 10) + 1); 
var answer=prompt("Enter a number you want to try");

while(secret_number!==Number(answer)) {
    // "" covers case of clicking OK with nothing entered
  // null covers case of clicking Cancel
    if(answer == null || answer == "") {
        break;
  }

    if(Number(answer)>secret_number) {
        answer = prompt("This number is too high!");
    } else if(Number(answer)<secret_number) {
        answer = prompt("This number is too low!");
    }
}
if (secret_number!==Number(answer)) {
    alert("You gave up :(");
} else {
    alert("You got it!");
}

Fiddle: https://jsfiddle.net/h10qt0nn/1/

The reason you have an endless loop is that you don't change answer 's value inside your loop. As a result each time the while condition is evaluated answer is still the first number entered into the prompt and the loop runs again. Also it's worth noting that if the user enters nothing answer will be set to an empty string ( "" )

working version:

var secret_number=Math.floor((Math.random() * 10) + 1); 
var answer=prompt("Enter a number you want to try");

while(secret_number!==Number(answer))
{
  if(answer===null || answer = ""){
    break;
  }

  if(Number(answer)>secret_number){
    answer = prompt("This number is too high!");
  }
  else if(Number(answer)<secret_number){
    answer = prompt("This number is too low!");
  }

  else{
    alert("You got it!");
  }
}

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