By Joe Burns
Use these to jump around or read it all
The Concept
The Script
The Script's Effect
Deconstructing the Script
What You've Learned
Your Assignment
The Concept
This example introduces you to random numbers. JavaScript uses the date and time when it generates a random number.
Notice this in the script: The number after the % is the ending number. The example below picks a random number between 1 and 10.
The Script
<HTML>
<HEAD>
<SCRIPT type="text/javascript">
function rand()
{
var now=new Date()
var num=(now.getSeconds())%10
var num=num+1
alert(num)
}
</SCRIPT>
</HEAD>
<BODY>
<h1>Random Numbers</h1>
<form>
<INPUT TYPE="button"
VALUE="Random Number from 1 to 10"
onClick="rand()">
</FORM>
</BODY>
</HTML>
|
The Script's Effect
Random Numbers
Deconstructing the Script
What You Have Learned
Your Assignment
Write a JavaScript program where the user clicks a button in a form and the program displays a random number between 0 and 4 (yes, 0 and 4). The number should be displayed in an alert box. The alert box should read "Your Random Number is: ##."
The Concept
The Script
The Script's Effect
Deconstructing the Script
What You've Learned
Your Assignment
|