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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Guessing Game Help.

    What I'm trying to achieve is it initialises a number between 1-2000 randomly.
    When the submit button is clicked it simply returns if the number that is in putted is higher or lower than the randomly initialised number.
    External JavaScript:

    Html
    Code:
    <html>
    <head>
    <script type="text/javascript" src="jsscri.js"></script>
    </head>
    <body onload="initialisation ()">
    
    <form name="exampleform">
     <h3>
    Number: <input name="number" type="text" 
                onchange='checkNumber( this.value );'>
    </h3>
     <p>
    	<input name="submit" type="button" value="Guess"
    	onclick= 'checkValue()';/>  
     </p>
     
    
    </body>
    </html>
    If you load this onto a webpage you'll see what i'm trying to do. I tried deleting the rest of the code to keep the size of the code down.
    Last edited by Jamesc; 12-14-2011 at 09:34 PM.

  • #2
    New Coder
    Join Date
    Nov 2011
    Location
    New England
    Posts
    62
    Thanks
    0
    Thanked 10 Times in 10 Posts
    "checkNumber" is not a function. Did you leave some code out?

    Also, you misspelled initialization as "initialisation" in the onload.
    Last edited by mvmacd; 12-14-2011 at 08:36 PM.
    For programming information, visit irnsystems.com
    Also check out Points2Survey and earn items.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Also, the checkValue( ) function requires a single argument passed to it.

    When invoked from the "Guess" button, no value is supplied.
    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
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Sorry. I changed the spelling. Heres the check function. It checks the in-putted text and alerts the user if the number they put is outside the range of 0-2000.

    Code:
    function checkNumber(num) 
    {
       
       if ( isNaN(num) || num < 0 || num > 2000) {
          alert( num + " Number has to be between 0 - 2000");
       }
    }
    Also, the checkValue( ) function requires a single argument passed to it.
    Code:
    <p>
    	<input name="submit" type="button" value="Guess"
    	onclick= 'checkValue(num)';/>  
     </p>
    Is this right? I'm not sure if it looks at the values in the input box then checks.
    Last edited by Jamesc; 12-14-2011 at 08:52 PM.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    How could num possibly be right?

    Look here:
    Code:
        <input name="number" ...
    So:
    Code:
    	<input name="submit" type="button" value="Guess"
    	    onclick="checkValue(this.form.number.value)" />
    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.

  • Users who have thanked Old Pedant for this post:

    Jamesc (12-14-2011)


  •  

    Posting Permissions

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