www.webdeveloper.com
Results 1 to 2 of 2

Thread: Why my last "for" doesn't work?

  1. #1
    Join Date
    Oct 2012
    Posts
    8

    Why my last "for" doesn't work?

    Why it doesn't write me the user guess number to finally say the user tried "x"times to guess the number?

    Thanks

    <HTML>
    <head><title>Javascript Exercices CSPS 130</title></head>
    <body>
    <h2><u> JavaScript Exercise 10 - RANDOM NUMBER GAME </u> </h2>
    <p>
    <script text="javascript">
    // Store the random number that the user has to guess

    var my_number = rand ( 100 );

    // Start a new game

    function new_game ( )
    {
    // Clear the guess form field
    document.getElementById("user-guess").value = "";

    // Generate a new random number
    my_number = rand ( 100 );

    // Tell the user that we're ready for them to guess
    alert ( "OK, I'm thinking of another number..." );
    }

    // Process the user's guess

    function make_guess ( )
    {
    // Get the user's guessed number
    var user_guess = document.getElementById("user-guess").value;

    // Warn if they haven't entered a number between 1 and 100

    if ( isNaN ( user_guess ) || user_guess < 1 || user_guess > 100 )
    {
    alert ( "Please enter a guess between 1 and 100" );
    return;
    }

    // Compare the guessed number against the computer's number,
    // and respond accordingly

    if ( user_guess > my_number )
    {
    alert ( "Too high - try again!" );
    }
    else if ( user_guess < my_number )
    {
    alert ( "Too low - guess a higher number!" );
    }
    else
    {
    alert ( "You got it! My number was " + my_number );
    new_game ( );
    }
    }
    for (var user_guess){
    document.write ("<br>" user_guess "</br>);

    if(my_number) {
    break;
    }

    }
    </script>
    <center>
    <input id="user-guess" type="text" size="2" value="" />
    <! button that the user can click to check their guess:>

    <input type="button" value="Guess" onclick="make_guess()" />
    </center>

    </body>
    </html>

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,330

    Lightbulb

    Couple of problems with your highlighted code:
    Code:
    for (var user_guess){
    document.write ("<br>" user_guess "</br>);
    
    if(my_number) {
    break;
    }
    You cannot use document.write anytime after the page has been initially rendered.
    Doing so causes the page to reload as if you had entered for the first time ever.

    Also your syntax for the string to display is invalid: Should be "<br>"+user_guess+"<br>" if you decide to display it elsewhere.

    Other problems exist as well. You should use the error console if you are testing with FF or Chrome browsers.

    Finally, you should enclose you script between [ code] and [ /code] tags (without the spaces)
    to make it easier for forum members to read, copy, test and retain proper formatting.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles