Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Guessing the random number function...

1. ## 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!!!

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;

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;}
}

}

• 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;

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;}
}

}[/QUOTE]

• Hi Bobo,

Do you happen to have any suggestions I could implement by any chance?

I'd really appreciate any help.

Thanks

Arun

• 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
•