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!!