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-09-2010, 03:22 AM   PM User | #1
omgwtfhtml
New to the CF scene

 
Join Date: Mar 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
omgwtfhtml is an unknown quantity at this point
JS numeric guessing game

Absolutely nothing happens when I click on the button, and I know NOTHING about Java. Need serious help!

The user is supposed to arbitrarily type a number (1-5) and see if it's the same one that JavaScript generates. The function "secretNum" is supposed to be a global variable, but I don't have a clue as to what that means. Anyways, an alert should pop up saying either congrats or try again.

Code:
<html>
<head>
<title>Guessing Game</title>
<script type="text/javascript">
function setUp ()
{
   secretNum = 1+Math.floor(5*Math.random());
   alert = (secretNum);
}

function checkGuess ()
{
	userGuess = document.IfForm.guessBox.value;
	userGuess = parseFloat(userGuess);
	// get value from text box

	if (secretNum == userGuess)
		  {alert ("Correct!");}
	else
		  {alert ("Sorry, Try Again");} 
}
</script>
</head>

<body style="text-align:center" onLoad="setUp();">
<h2>Guess the Number From 1 to 5</h2>
		<form name="IfForm">
Enter Your Guess:
<input type="text" size="5" name="guessBox" value="" />
<br />
<input type="button" value="Check Guess" onClick="checkGuess" />
		</form>
</body>
</html>
omgwtfhtml is offline   Reply With Quote
Old 03-09-2010, 03:50 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,863
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
Code:
   alert = (secretNum);
what’s that? well, it overwrites the window.alert() method, that’s why you get errors.

one of the reasons why globals are evil …
__________________
please post your code wrapped in [CODE] [/CODE] tags

Last edited by Dormilich; 03-09-2010 at 03:55 AM..
Dormilich is offline   Reply With Quote
Old 03-09-2010, 06:01 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Also, this is wrong:
onClick="checkGuess"

Needs to be
onClick="checkGuess( )"
with the parentheses.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 03-09-2010, 07:32 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by omgwtfhtml View Post
I know NOTHING about Java.
Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

For an explanation of variables with global and local scope, see:-
http://www.webdevelopersnotes.com/tu...avascript.php3

Last edited by Philip M; 03-09-2010 at 07:47 AM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
alert, function, javascript, random, variable

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 05:21 PM.


Advertisement
Log in to turn off these ads.