views:

90

answers:

0

i have a code from book like this :

<script type="text/javascript">
        <!-- start hiding javascript
        var randomNo = 0;
        var gameOver = false;
        var keepPlaying = true;
        var guess = "";
        var msg = "I'm thinking of a number between 1 to 10, can you guess it?";
        var reply = "";

        while (gameOver == false){ //as long as it's not game over
            randomNo = 1 + Math.random() * 9;
            randomNo = Math.round(randomNo);
            keepPlaying = true; // set value that indicate current round of play should continue

            while(keepPlaying == true){ // player still want to play
                //prompt the player to guess the secret number
                guess = window.prompt(msg + "(" + randomNo + ")");

                //analyse the player guess
                if (guess == randomNo){
                    window.alert("correct. you guessed the number");
                    reply = window.prompt("Would you like to play again?(y/n)")
                    if(reply == "y"){
                        keepPlaying = true;
                        gameOver = false;                           
                    }else{
                        keepPlaying = false;
                        gameOver = true;
                    }
                } else {
                    if(guess > randomNo){
                    window.alert("not correct. your guess is too high");
                    } else {
                        window.alert("Incorrect. your guess is too low");
                    }
                } 

            }
        }
        window.alert("thankyou for playing");
        -->
    </script>

and when i try, it works fine... but when i add more option in if statement like this :

while(keepPlaying == true){ // player still want to play
                //prompt the player to guess the secret number
                guess = window.prompt(msg + "(" + randomNo + ")");

                //analyse the player guess
                if(guess > randomNo){
                    window.alert("not correct. your guess is too high");
                    } 
                if(guess < randomNo){
                    window.alert("Incorrect. your guess is too low");
                }
                if (guess == randomNo){
                    window.alert("correct. you guessed the number");
                    reply = window.prompt("Would you like to play again?(y/n)")
                    if(reply == "y"){
                        keepPlaying = true;
                        gameOver = false;                           
                    }else{
                        keepPlaying = false;
                        gameOver = true;
                    }
                }
            }

or i change to :

if (guess == randomNo){
                    window.alert("correct. you guessed the number");
                    reply = window.prompt("Would you like to play again?(y/n)")
                    if(reply == "y"){
                        keepPlaying = true;
                        gameOver = false;                           
                    }else{
                        keepPlaying = false;
                        gameOver = true;
                    }
                } else if(guess > randomNo){
                    window.alert("not correct. your guess is too high");
                    } else{
                        window.alert("Incorrect. your guess is too low");
                    }

it makes the processor work load on firefox more than 80%. note: i see the processor workload from the task manager. i change the if model because i will add extra option

can anyone help me? thanks in advance