JavaScript Primers #23

By Joe Burns

http://www.htmlgoodies.com/primers/jsp/article.php/3478361/JavaScript-Primers-23.htm (Back to article)

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

Before we go on to a new topic, let's look at one more example of IF. It's important to feel very comfortable with this branching technique which lets you create more creative and more interactive programs.


The Script

<HTML>
<BODY>
<SCRIPT type="text/javascript">
   var0="An Apple A Day"
   var1="A Stitch in Time"
   var2="Bird in the Hand"
  now=new Date()
  num=(now.getSeconds() )%3

  document.write("Random Number: " 
    + num + "<br>")

  if (num == 0)
    {cliche=var0}
  if (num == 1)
    {cliche=var1}
  if (num == 2)
     {cliche=var2}
  document.write(cliche)
</SCRIPT>
<p>....as I always say.

</BODY>
</HTML>


The Script's Effect

My Home Page

...as I always say.

(Press Reload several times to see different random numbers and quotes.)


Deconstructing the Script


What You Have Learned


Your Assignment

   

Modify this JavaScript program to display a random picture. Right click on pic1.gif, pic2.gif, and pic3.gif, above, to save them to your work area. Under the picture, the page should display "...describes my mood today."

Here's a possible answer to this assignment
(this will open a new window)


The Concept
The Script
The Script's Effect
Deconstructing the Script
What You've Learned
Your Assignment

On To JavaScript Primer #24