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>:-
And this in the <body>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>
Explanation:-Code:<form name="form1"> Enter a number <input type="text" size=5 maxlength=3 name="num"> <input type="button" onClick="guessnum();" value="Enter"> </form>
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 pointsCode:var num = Math.floor(Math.random() * 101);
A Preview:-
Conclusion:-
As Always Feedback is welcome and the full source is attached!!
thanks man for this code
wow! javaScript is amazing! thanks a lot!
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
What code section? You mean the code tags are empty??
Well they are not empty for me :S
Last time I was here, the code tags were empty.
Maybe because you did not have enough posts to view the code in the threads?
Wait you have to have a certain number of posts to see code?
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.
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.
actually the counter doesn't really work, it only gets to 1 guesses and doesn't work, could someone help me fix this help?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>
Last edited by chili5; 03-24-2008 at 09:16 AM.
There are currently 2 users browsing this thread. (0 members and 2 guests)
Bookmarks