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 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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>

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    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 …
    Last edited by Dormilich; 03-09-2010 at 03:55 AM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,426 Times in 4,391 Posts
    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.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

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