4
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> 
<head>
<title>Game</title>
<script type="text/javascript" src="game.js"></script>

</head>
<body>
    <h1>GameSite</h1>
    <p> This program will generate a random number. Please input your guess in the box!</p>
    <button onclick="guessGame()">Press me to play a quick game!</button>

<script>
    function guessGame(){
        number = Math.floor(Math.random()*11);
        document.write(number);
        var guess = prompt("Guess a number: ");
            do {
                guess = prompt("Keep guessing!");
                if (number < guess) {
                    prompt("You've guessed too high!");
                } else if (number > guess) {
                    prompt("You've guessed too low!");
                } else document.write("Good Job!");
            } while (guess != number);
    }
    </script>
    </body> 
</html>

I am having trouble with the loop. It loops fine but if I move the guess statement too after the if statement it goes back to that each time. If I put it outside the if statement then it seems to not actually allow me to guess a valid number. It's hard to explain :\

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
Kacey Gambill
  • 107
  • 1
  • 4

4 Answers4

5

I would suggest just to use only one prompt and assign it to the variable.

function guessGame() {
    var number = Math.random() * 11 | 0,
        guess,
        text = 'Guess a number:';
    do {
        guess = prompt(text);
        if (number < guess) {
            text = "You've guessed too high!";
        } else if (number > guess) {
            text = "You've guessed too low!";
        }
    } while (guess != number);
    document.write("Good Job!");
}
guessGame();
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Try this:

function guessGame(){
    number = Math.floor(Math.random()*11);
    document.write(number);
    var guess = prompt("Guess a number: ");
    while (guess != number) {
        if (number < guess) {
            guess = prompt("You've guessed too high! Keep guessing!");
        } else {
            guess = prompt("You've guessed too low! Keep guessing!");
        }
    }
    document.write("Good Job!");   
}

The while loop will keep running until the correct number is guessed, and then it will terminate.

edit: sorry, mistakes from typing too fast

eddyjs
  • 1,270
  • 10
  • 20
  • 1
    The `!==` operator is really nasty joke from your side: since prompt result is string and number is integer, the user will be doomed to guess forever. – Jan Turoň Jul 31 '15 at 08:48
  • Oh thanks for that correction Jan, I'm so used to just always using !== that I glazed over that. – eddyjs Jul 31 '15 at 08:49
  • Why did you remove it? I would keep it, so the game would be much more fun >:-D – Jan Turoň Jul 31 '15 at 08:49
0

I'd recommend use alert when you don't need user's response

function guessGame(){
    number = Math.floor(Math.random()*11);
    var guess;
        do {
            guess = prompt("Guess a number: "); // you should handle string input here
            if (number < guess) {
                alert("You've guessed too high!");
                alert("Keep guessing!");
            } else if (number > guess) {
                alert("You've guessed too low!");
                alert("Keep guessing!");
            } 
        } while (guess != number);
        alert("Good Job!");
}
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
0

Trust me, I'm a programmer.

    function guessGame() {
    guess:
            var number = Math.floor(Math.random() * 11);
            document.write(number);
            var guess;// = prompt("Guess a number: ");
            var text = 'Guess a number:';
            guess = prompt(text);
            if (number == guess) {
                document.write("Good Job!");
                return true;
            } else {
            if (number < guess) {
                text = "You've guessed too low!";
            } else {
                text = "You've guessed too high!";
            }
                goto guess;
            }
}
        guessGame();