View Single Post
  #1 (permalink)  
Old 12-05-2006, 03:05 PM
TcM's Avatar   
TcM TcM is offline
Moderator
 
Join Date: Aug 2006
Location: In a technologic world :p
Posts: 7,360
Rep Power: 67
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 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 To view attachments your post count must be 1 or greater. Your post count is 0 momentarily.
Reply With Quote

Sponsored Links