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
    Aug 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    3 guess number page

    I need help with a program Im trying to write. This program is a three guess program, guessing numbers between 1-10 having three guesses and telling you higher or lewer then after the third time says sorry the correct answer is (num) I have this so far

    <html>
    <head>
    <title>Guess Number Three Times</title>
    <script language ="Javascript">
    <!--
    function makeGuess(num)
    {var guess = parseInt(prompt('Guess number from 1 to 10',' '));
    if(guess == num) alert('Correct!');
    esle (guess < num) alert('The number is more than' + num);
    esle if (guess > num) alert('The number is less than' + num);
    esle alert('Sorry wrong guess it was' + num);
    }//end makeGuess
    //-->
    </script>
    </head>
    <body>
    <center>
    <h1>Guess The Right Number</h1>
    <h2>Welcome to the guessing game!</h2>
    <h3>Guess a Number between 1 and 10</h3>
    </center>
    <script language = "javascript">
    <!--
    var num = (Math.floor(Math.random()*10))+1;//rolldice
    //-->
    </script>

    <center>
    <input type="button"
    value="Make a Guess"
    onClick="makeGuess(num)">
    </center>

    </body>
    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,318
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    My guess is that you cannot spell "else".

    Code:
    <html>
    <head>
    <title>Guess Number Three Times</title>
    <script language ="Javascript">
    <!--
    function makeGuess() {
    
    var num = (Math.floor(Math.random()*10))+1;//rolldice
    
    for (var i = 1; i<=3; i++) {
    var guess = parseInt(prompt('Guess number from 1 to 10',' '));
    
    if (guess <1 || guess >10) {
    alert ("Not a valid number - retry!");
    guess = 0;
    i--;
    }
    
    if(guess == num) {
    alert('Correct!  The number was ' + num);
    i = 4;  // = break
    }
    
    if (i < 3 && guess != 0 && guess < num) {
    alert('The number is more than ' + guess);
    }
    
    if (i < 3 && guess > num) {
    alert('The number is less than ' + guess);
    }
    
    }
    
    if (guess != num) {
    alert('Sorry, wrong guess! It was ' + num); 
    }
    }
    //-->
    </script>
    
    </head>
    <body>
    
    <center>
    <h1>Guess The Right Number</h1>
    <h2>Welcome to the guessing game!</h2>
    <h3>Guess a Number between 1 and 10</h3>
    <input type="button" value="Make a Guess" onClick="makeGuess()">
    </center>
    
    </body>
    </html>
    Last edited by Philip M; 08-19-2007 at 02:14 PM.


  •  

    Posting Permissions

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