www.webdeveloper.com
Results 1 to 9 of 9

Thread: Help with Javascript code..guessing game...

  1. #1
    Join Date
    Apr 2012
    Posts
    6

    Help with Javascript code..guessing game...

    I didnt remember what the document.write thing was for in the for loop and I also wasnt sure what the code would be to get the numbers guessed from the text box to compare them with the random number. I know something else is wrong to, but Im not sure what. Im a total newbie at this stuff. This is what I have so far:

    function startGame ( )
    {
    var ranNum = Math.floor((Math.random()*50)+1);
    document.getElementById("t1").value = ranNum
    var i=0;
    for (i=0;i<=10;i++)
    {
    document.write("Hello" + i + "<br>");
    }
    var answer = prompt("Guess a number between 1 and 50!");
    if(answer > random)
    {
    alert("Guess lower!");
    continue;
    }
    else if(answer < random)
    {
    alert("Guess higher!!");
    continue;
    }
    else if(answer==random)
    {
    alert("Correct!");
    break;
    }
    }

    </script>
    </head>
    <body>
    <form name="aform" id="aform">
    <strong> Guess a random number </strong>
    <input type="text" name="t1" id="t1">
    <input type="button" name="b1" id="b1" value="Generate random number"
    onclick="startGame()">

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,257

    Lightbulb

    You cannot use document.write() after the page has been rendered.
    If you try to use it, the script is reloaded and you start all over again.
    Essentially you will get ONLY ONE guess at your game, period.

    That assumes it would run at all because there is no setting of the variable 'random'
    which is what you are trying to compare to with your nested IF statements.

    Note the following changes (trying to keep the spirit of your original design)
    and compare it to your design. It would also behoove you to use the error console
    if you are using the FF or Chrome browsers for testing.
    Code:
    <html>
    <head>
    <title> JS guessing game </title>
    <script type="text/javascript">
    var ranNum;
    function playGame ( ) {
      var answer = prompt("Guess a number between 1 and 50!");
      if(answer > ranNum) { alert("Guess lower!"); }
      if(answer < ranNum) { alert("Guess higher!!"); }
      if(answer == ranNum) { alert("Correct!"); return true; }
      return false;
    }
    function generateRandomNumber() {
      ranNum = Math.floor(Math.random()*50)+1;
    }
    window.onload = function() { generateRandomNumber(); }
    </script>
    </head>
    <body>
    <strong> Guess a random number </strong>
    <input type="button" value="Play game" onclick="playGame()"> 
    <input type="button" value="New game" onclick="generateRandomNumber()"> 
    </body>
    </html>
    Finally you should enclose your scripts between [ code] and [/ code] tags
    without the spaces to make it easier to spot your code in the thread.

  3. #3
    Join Date
    Apr 2012
    Posts
    6
    Thank you so much JMRKER!! I was so confused my teacher made it sound as if we were supposed to have one function, but now I see why I needed to have two which makes total sense. Now how would I do a while loop that lets the user guess 10 times instead of an infinite number of times? I was confused on that too because I dont now if its part of the if statement or not.

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,257

    Lightbulb

    Not the way I would do it, but still trying to keep with your particular program logic...
    Code:
    <html>
    <head>
    <title> JS guessing game </title>
    <script type="text/javascript">
    var ranNum;
    function playGame ( ) {
      var answer;
      var fnd = false;
      var cnt = 10;
      while ((cnt > 0) && (fnd == false)) {
        answer = prompt("Guess a number between 1 and 50!");
        if (answer > ranNum) { alert("Guess lower!"); }
        if (answer < ranNum) { alert("Guess higher!!"); }
        if (answer == ranNum) { alert("Correct!"); fnd = true; }
        cnt--;
      }
      if (!fnd) { alert('Too bad, you lose ... The number was '+ranNum); }
      return fnd;
    }
    function generateRandomNumber() {
      ranNum = Math.floor(Math.random()*50)+1;
    }
    window.onload = function() { generateRandomNumber(); }
    </script>
    </head>
    <body>
    <strong> Guess a random number </strong>
    <input type="button" value="Play game" onclick="playGame()"> 
    <input type="button" value="New game" onclick="generateRandomNumber()"> 
    </body>
    </html>
    Make sure you can explain all the parts to your instructor!

  5. #5
    Join Date
    Apr 2012
    Posts
    6
    Thanks again JMRKER! What does fnd stand for? Im guessing cnt stands for count? So for the last statement !fnd. Does that mean that if fnd is true then that is the number that was guessed correctly, but if its false that means that after 10 tries through the loop you didnt guess the correct number? Im not sure what ! means I looked it up and it said that it toggles a statement from false to true or true to false. Sorry Im just trying to make sure I totally understand it.

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,633
    fnd stands for "found" ever a russian can get it lol )))
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  7. #7
    Join Date
    Apr 2012
    Posts
    6
    Ok thanks! I dont really know anything about javascript Im a total novice. Sorry.

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,257

    Arrow

    Quote Originally Posted by Ilovefutbol View Post
    Thanks again JMRKER! What does fnd stand for? Im guessing cnt stands for count? So for the last statement !fnd. Does that mean that if fnd is true then that is the number that was guessed correctly, but if its false that means that after 10 tries through the loop you didnt guess the correct number? Im not sure what ! means I looked it up and it said that it toggles a statement from false to true or true to false. Sorry Im just trying to make sure I totally understand it.
    Quote Originally Posted by Padonak View Post
    fnd stands for "found" ever a russian can get it lol )))
    In the words of John Wayne: "Yep".

    fnd was abbreviation for found.
    In this case, fnd defined as true means found and fnd as false means not-found.
    !fnd means false or not-found when the value of fnd is true.

    It really gets squirely if you define fnd = false.
    Then !fnd would be true, but it makes reading the code a bit confusing.
    I suggest using the assignment that makes the most sense to you and stick with it!

  9. #9
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,633
    it's ok i was just trollin <wink>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles