View Single Post
  #14 (permalink)  
Old 03-25-2008, 05:30 PM
chili5's Avatar   
chili5 chili5 is offline
Code Warrior
 
Join Date: Mar 2008
Age: 15
Posts: 3,446
Credits: 309
Rep Power: 30
chili5 is a glorious beacon of lightchili5 is a glorious beacon of lightchili5 is a glorious beacon of lightchili5 is a glorious beacon of lightchili5 is a glorious beacon of lightchili5 is a glorious beacon of light
Default Re: JavaScript:Tutorial, Your First Game!

here is my finished code, it uses the div id="counter" to show how many guesses up to date so far.

HTML Code:
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
var num = Math.floor(Math.random() * 101);
var count = 0;
function guessnum()
		{
			var guess = do***ent.forms['form1'].num.value;
			if (guess == num)
				{
					count+=1;
					alert("Great you Guessed! took you" + count);
					do***ent.getElementById('counter').innerHTML = "Congrats you won, it took you " + count + " guesses to win.";
				}
			if (guess < num)
				{
					count+=1;
					alert("No your number is too low! Count " + count);
do***ent.getElementById('counter').innerHTML = "No your number is too low " + count + " guesses so far.";
				}
			if (guess > num)
				{
					count+=1;
					alert("No your number is too  high Count " + count);
do***ent.getElementById('counter').innerHTML = "No your number is too high " + count + " guesses so far.";
				}
		}
</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>
Reply With Quote