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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Function not defined error in basic program

    Just learned javascript today, and I just can't get my head around why this program doesn't work. Here is a link to the code (syntax colored, numbered, etc.)

    The code is just to play a number guessing game. I have two functions, playGame() and guessfunction(). playGame() just starts playing the game,and when the user clicks the button, it calls guessfunction(), which checks to see how the answer relates to the random number generated at the beginning (>,<,=, etc). If the guess and the random number aren't equal, it calls playGame() again. However, once playGame() gets called a second time, I get a "function not defined" error for guessfunction(). Why is this occuring?

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,133
    Thanks
    38
    Thanked 504 Times in 498 Posts
    Can you post your script. The link is helpful but it does not contain your problem.

  • #3
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    As an aside, your random number guesser chooses between 0 and 100.

    You want to do this instead:

    Code:
    var randomnumber=Math.floor(1 + Math.random()*100);

  • #4
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jmrker View Post
    Can you post your script. The link is helpful but it does not contain your problem.
    The link is the code for my entire index.html file. There isn't another script or any other code.

  • #5
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by timo1023 View Post
    If the guess and the random number aren't equal, it calls playGame() again. However, once playGame() gets called a second time, I get a "function not defined" error for guessfunction(). Why is this occuring?
    Because use of document.write after the document has finished loading, overwrites the existing content.
    Re-structure the program so that the form is only written once.

  • #6
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Great, thanks guys, I got it working.

    I ended up writing "too high" or "too low" to an input text box. Is there a way to write text to the screen without overriding everything else and without writing to a form value?

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,252
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Quote Originally Posted by timo1023 View Post
    Great, thanks guys, I got it working.

    I ended up writing "too high" or "too low" to an input text box. Is there a way to write text to the screen without overriding everything else and without writing to a form value?
    Code:
    <div id = "mydiv"></div>
    
    <script type = "text/javascript">
    if (chosenNumber < correctNumber) {
    document.getElementById("mydiv").innerHTML = "Too low!";
    }
    if (chosenNumber > correctNumber) {
    document.getElementById("mydiv").innerHTML = "Too high!";
    }
    if (chosenNumber == correctNumber) {
    document.getElementById("mydiv").innerHTML = "You got it!";
    }
    </script>
    Naturally chosenNumber and correctNumber need to be changed to the actual names of the relevant variables.


    The Pastor will preach his farewell message, after which the choir will sing "Break forth into Joy" - Church bulletin
    Last edited by Philip M; 09-13-2008 at 06:38 PM. Reason: Noticed typo


  •  

    Posting Permissions

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