View Single Post
  #11 (permalink)  
Old 03-25-2008, 07:11 AM
TcM's Avatar   
TcM TcM is offline
Moderator
 
Join Date: Aug 2006
Location: In a technologic world :p
Posts: 7,348
Rep Power: 66
TcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud ofTcM has much to be proud of
Default Re: JavaScript:Tutorial, Your First Game!

Well I don't know why, but your code does not show any dialog, but it's pretty simple, your value of count is remaining 1 because. you are declaring it as 0 within the function, so whenever you press the button it declares it 0 and then increases it by 1, then again you press the button and it declares it as 0 and increases it by one, so just declare out of the function it's self, below the num variable.

I made this code for you hope it helps:

Code:
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
var num = Math.floor(Math.random() * 101);
var count = 0;
function guessnum()
		{
			var guess = document.forms['form1'].num.value;
			if (guess == num)
				{
					count+=1;
					alert("Great you Guessed! took you" + count);
				}
			if (guess < num)
				{
					count+=1;
					alert("No your number is too low! Count " + count);
				}
			if (guess > num)
				{
					count+=1;
					alert("No your number is too  high Count " + 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>
Reply With Quote