...

View Full Version : Guessing the random number function...



asharm27
03-21-2005, 01:33 AM
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!!! :confused:

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!!");
}

}

Bobo
03-21-2005, 01:54 AM
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]

asharm27
03-21-2005, 02:04 AM
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

Brandoe85
03-21-2005, 11:28 AM
You can play with this and change it to fit your needs:


<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; :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum