Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-21-2005, 12:33 AM   PM User | #1
asharm27
New to the CF scene

 
Join Date: Mar 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
asharm27 is an unknown quantity at this point
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!!");
}

}
asharm27 is offline   Reply With Quote
Old 03-21-2005, 12:54 AM   PM User | #2
Bobo
Regular Coder

 
Join Date: Jan 2004
Location: Port Huron, MI, U.S.A.
Posts: 280
Thanks: 0
Thanked 0 Times in 0 Posts
Bobo is an unknown quantity at this point
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? ........
Bobo is offline   Reply With Quote
Old 03-21-2005, 01:04 AM   PM User | #3
asharm27
New to the CF scene

 
Join Date: Mar 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
asharm27 is an unknown quantity at this point
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
asharm27 is offline   Reply With Quote
Old 03-21-2005, 10:28 AM   PM User | #4
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
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;
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:17 PM.


Advertisement
Log in to turn off these ads.