Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Location
    Atlanta, ga
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Help with a while loop

    Im in my first scripting class, I am supposed to code a number guessing game with a while loop that asks the user if they want to play the game..
    this is what I have so far..
    what do I need to do ?
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>
    <title>Hot or cold Guessing Game</title>

    </head>
    <body>

    <script type="text/javascript">
    /*
    The code below will pop up an alert box
    and ask you to guess the number, it will then
    let user know if hot or cold and when you win it
    will calculate the number of guesses it took the user to get to the answer
    */
    var random_number = parseInt(Math.random() * 100, 10);
    var guesses = 0;
    var last_guess = 0;
    var message = "";
    var user_response ="yes";

    while (user_response !="yes"){
    user_response = prompt ("Do you want to play again?");
    if (user_response == "yes") { HAVING A PROBLEM HERE !=CONTINUE GAME;
    { else if (user_response=="") { End Game Here ;
    }



    while (last_guess != random_number) {
    guesses++;
    if (last_guess == 0) >= (random_number - 20) && last_guess <= (random_number + 20)) {
    message = "You're getting cold.";
    } else {
    message = "Ice Cold!";
    }
    last_guess = parseInt("0" + prompt(message, last_guess), 10);
    }

    message = "Success! You Win!\n";
    if (guesses < 5) {
    message += "You did an awesome job, it only took you " + guesses + " tries to guess the number!";
    } else if (guesses <= 15) {
    message += "Goodness. It took you " + guesses + " tries to guess the target number.";
    } else {
    message += "Yikes! It took you " + guesses + " tries to guess the target number.";
    }
    alert(message);
    {

    } // end if
    } // end while
    //]]>
    </script>

    </body>
    </html>

  • #2
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Code:
    var user_response ="yes";
    while (user_response !="yes"){
     	user_response = prompt ("Do you want to play again?");
    	if (user_response == "yes") { HAVING A PROBLEM HERE !=CONTINUE GAME;
    	{ else if (user_response=="") { End Game Here ; }
    Simple answer is you just jump out while loop before it start.
    To fix this problem is just change "yes" at var user_response to "no" or something else except "yes" and "".


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •