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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Guessing the random number function...

    Hi am am doing a bit of research and came up with a little game to demonstrate my website to school kids.

    My idea is that they type in a number in a text box and then they click a button to see if their guess matches the random number. If their guess is too low they get a window.prompt saying the no. is too low please enter another. If this guess is too low or high they get the same kind of window.

    I have made an attempt at the code but for some reason it doesnt work. I'd like to keep the code relatively simple (if possible) so that I can explain it to them.

    Is there any way someone might be able to help me please as i'm new at this game and pretty overwhelmed!!!

    Thanks in advance

    Arun

    The guess is entered in a simple text box in a form named 'form' and the action button then calls this function.

    function random(){

    var temp;
    var randGuess;
    var random;
    var randomInt;
    var at1;
    var at2;

    at1 = window.prompt("Your guess is too low, please enter another guess","0");
    at2 = window.prompt("Your guess is too high, please enter another guess","0");
    random = math.floor(math.random()*11);
    randomInt = parseInt(random);
    randGuess = parseInt(document.form.randomNo.value);

    if(randGuess=randomInt){window.alert("Congratulations, you have guessed the random number correctly!!!")}
    if(temp=randomInt){window.alert("Congratulations, you have guessed the random number correctly!!!")}
    if(randGuess>randomInt) {temp = at2;}
    else{ if(randGuess<randomInt) {temp = at1;}
    else window.alert("Correct!!");
    }

    }

  • #2
    Regular Coder
    Join Date
    Jan 2004
    Location
    Port Huron, MI, U.S.A.
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not testing the code or anything , I noticed some syntax errors on youyr script. The color is the correction



    function random(){

    var temp;
    var randGuess;
    var random;
    var randomInt;
    var at1;
    var at2;

    at1 = window.prompt("Your guess is too low, please enter another guess","0");
    at2 = window.prompt("Your guess is too high, please enter another guess","0");
    random = Math.floor(Math.random()*11);
    randomInt = parseInt(random);
    randGuess = parseInt(document.form.randomNo.value);

    if(randGuess==randomInt){window.alert("Congratulations, you have guessed the random number correctly!!!")}
    if(temp==randomInt){window.alert("Congratulations, you have guessed the random number correctly!!!")}
    if(randGuess>randomInt) {temp = at2;}
    else{ if(randGuess<randomInt) {temp = at1;}
    else window.alert("Correct!!");
    }

    }[/QUOTE]
    Oh, was I supposed to put something here? ........

  • #3
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Bobo,

    I made the adjustments and still the code doesn't work.
    Do you happen to have any suggestions I could implement by any chance?

    I'd really appreciate any help.

    Thanks

    Arun

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You can play with this and change it to fit your needs:
    PHP Code:
    <html>
    <
    head>
    <
    script type="text/javascript">
    var 
    num Math.round(Math.random() * 11);   //Random number
    function guess()
    {
        var 
    input parseInt(document.form.randomNo.value);  //The number guessed
        
    var ran document.form.randomNo;
        if(
    input == num)   //Check to see if they guessed right
        
    {
            var 
    yesNo confirm("You guessed the right number!\nWould you like to play again?");  //prompt to ask if they want to play again
            
    if(yesNo)    //if they chose to play again
            
    {
                
    location.reload();    //reload game
                
    ran.value '';        //set the value to nothing
                
    return true;
            }
            else
            {
                return 
    false;        //they didn't want to play again
            
    }
        }

        if(
    input num)        //the number is to high
        
    {
            
    alert("Your guess is to high! try again");
            
    ran.select();    //give focus to the text box
        
    }

        if(
    input num)        //the number is to low
        
    {
            
    alert("Your guess is to low! try again");
            
    ran.select();    //give focus to text box
        
    }
    }

    </script>
    </head>
    <body>
    <form name="form">
    <input type="text" name="randomNo">
    <input type="button" value="Guess" onclick="return guess();">
    </body>
    </html> 
    Good luck;


  •  

    Posting Permissions

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