+ Reply to Thread
Page 1 of 4 123 ... LastLast
Results 1 to 10 of 36

Thread: JavaScript:Tutorial, Your First Game!

  1. #1
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101

    JavaScript:Tutorial, Your First Game!

    Introduction:-
    Here I will show you how to make a simple 'Guess The Number' game. It will do fine as your first game!

    Solution:-
    This goes in the <head>:-
    Code:
    <SCRIPT LANGUAGE="JavaScript">
    var num = Math.floor(Math.random() * 101);
    function guessnum(){
    var guess = document.forms['form1'].num.value;
    if (guess == num)
    {
    alert("Great you Guessed! How did you know that?");
    }
    if (guess < num)
    {
    alert("No your number is too low!");
    }
    if (guess > num)
    {
    alert("No your number is too  high");
    }
    }
    </SCRIPT>
    And this in the <body>
    Code:
    <form name="form1">
    Enter a number <input type="text" size=5 maxlength=3
    name="num"> <input type="button" onClick="guessnum();"
    value="Enter">
    </form>
    Explanation:-
    Code:
    var num = Math.floor(Math.random() * 101);
    This is a Mathematical Function Math.Random() is to choose a random number *101 means up to 100 so the numbers will be from 0 to 100 and Math.floor is so to eliminate any decimal points

    A Preview:-


    Conclusion:-
    As Always Feedback is welcome and the full source is attached!!
    Attached Files Attached Files

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    ahmed_14_1 is offline Newbie
    Join Date
    Nov 2007
    Posts
    1
    Rep Power
    0
    thanks man for this code

  4. #3
    $RaMRoM$ is offline Newbie
    Join Date
    Oct 2007
    Posts
    6
    Rep Power
    0
    wow! javaScript is amazing! thanks a lot!

  5. #4
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Thumbs up Re: JavaScript:Tutorial, Your First Game!

    I don't see anything in the code section.

    I've seen this before its really cool though and very easy to change the range for the random number.

    ----------------
    Now playing: Disturbed - I'm Alive
    via FoxyTunes

  6. #5
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101

    Re: JavaScript:Tutorial, Your First Game!

    What code section? You mean the code tags are empty??

    Well they are not empty for me :S

  7. #6
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: JavaScript:Tutorial, Your First Game!

    Last time I was here, the code tags were empty.

  8. #7
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101

    Re: JavaScript:Tutorial, Your First Game!

    Maybe because you did not have enough posts to view the code in the threads?

  9. #8
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: JavaScript:Tutorial, Your First Game!

    Wait you have to have a certain number of posts to see code?

  10. #9
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101

    Re: JavaScript:Tutorial, Your First Game!

    Well I don't know... but the code was always there... thats why I asked you... can't understand why you did not see it.

  11. #10
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: JavaScript:Tutorial, Your First Game!

    Well it's working for me now. I was trying to add something to this, to keep the number of guesses. Here's my code, i think i got it working where it shows the number of guesses.

    HTML Code:
    <html>
    <head>
    <SCRIPT LANGUAGE="JavaScript">
    var num = Math.floor(Math.random() * 101);
    function guessnum(){
    var guess = document.forms['form1'].num.value;
    var count = 0;
    if (guess == num)
    {
    alert("Great you Guessed! How did you know that?");
    count += 1
    document.getElementById('counter').innerHTML = "Congratulations! you guessed the number! 
    
    It took you " + count + " guesses to win.";
    }
    if (guess < num)
    {
    alert("No your number is too low!");
    count += 1;
    document.getElementById('counter').innerHTML = "Number of guesses: " + count;
    
    }
    if (guess > num)
    {
    alert("No your number is too  high");
    count += 1;
    document.getElementById('counter').innerHTML = "Number of guesses: " + count;
    
    }
    }
    </SCRIPT>
    <title>Guess the Number</title>
    </head>
    <body>
    <form name="form1">
    Enter a number <input type="text" size=5 maxlength=3
    name="num"> <input type="button" onClick="guessnum();"
    value="Enter">
    </form>
    <div id="counter">
    
    </div>
    </body>
    </html>
    actually the counter doesn't really work, it only gets to 1 guesses and doesn't work, could someone help me fix this help?
    Last edited by chili5; 03-24-2008 at 09:16 AM.

+ Reply to Thread
Page 1 of 4 123 ... LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. JavaScript:Tutorial, Confirmation Box
    By TcM in forum JavaScript Tutorials
    Replies: 5
    Last Post: 11-07-2009, 08:13 PM
  2. JavaScript:Tutorial, Prompt
    By TcM in forum JavaScript Tutorials
    Replies: 2
    Last Post: 01-21-2007, 01:58 AM
  3. JavaScript:Tutorial, Loops
    By TcM in forum JavaScript Tutorials
    Replies: 4
    Last Post: 12-13-2006, 06:53 AM

Tags for this Thread

Bookmarks

Posting Permissions

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