Listado de conformidad, ítem tomando en referencia de la normativa ISO 9001:2015, (Medidas correctivas)
3 Metodología:
3.2 Procedimiento:
As referenced in Lessons 32 and 33, producing random numbers is good, but by itself, the technique doesn’t do much besides post a number. Here we start to take that concept of randomness and apply it to other items. In this case, one of three statements will pop up on the screen—which statement depends on which random number the computer comes up with.
In Lesson 21 in Chapter 4, “Flipping Images and Opening Windows with Mouse Events,”
you learned the concept of ifandelse. Here we use that concept once again to get the desired effect:
You can see the script’s effect in Figure 6.5.
To see the effect on your own computer, click Lesson Thirty-Four Script’s Effect in your down-load packet, or see it online at http://www.htmlgoodies.com/JSBook/lesson34effect.
html.
JavaScript Goodies
162
Deconstructing the Script
This one reads very nicely from the top down. Here we start by giving three lines of text three variable names var0throughvar2:
var0=”Apple a Day”;
var1=”A Stitch in Time”;
var2=”Bird in the Hand”;
We could have started with 1, but that would have required setting the random number generator code to add 1. It’s not worth the concern, so we’re just starting with 0. Moving along:
now=new Date()
num=(now.getSeconds() )%3
Next, the script uses the two-line date code to draw a random number between 0 and 2.
Note the number 3after the %sign. Remember that means the numbers 0, 1, and 2 could come up.
The code has assigned the variable name nowto the new date object and the variable num the result of the random number code.
Finally, this line writes the random number to the page through a document.writestatement:
document.write(“Random Number: “+ num + “<br>”)
Figure 6.5 Random statement.
Getting the Random Statement
Now, let’s look at the second section of the JavaScript:
if (num == 0)
{cliche=var0}
if (num == 1)
{cliche=var1}
if (num == 2)
{cliche=var2}
document.write(cliche)
The code that assigned the variable and chose a random number is now used to choose one of these three statements. Let’s take a look at just the first two lines of code:
if (num == 0)
{cliche=var0}
This is a statement using the ifmethod. Remember from Lesson 21 that any statement that follows ifmust be sitting inside parentheses.
Those two lines mean this: “If the number, num, created by the random number code is 0, setclicheequal to variable var0.”
Notice the cliche=statement is within braces ({}). The reason is that it’s actually a func-tion of the ifmethod.
But you remembered that from Lesson 21.
Double and Single Equal Signs
You might have caught this already, but it’s very important, so we’ll drive the point home.
In JavaScript, a double equal sign actually means is equal to.
A single equal sign simply acts as the verb is. Remember that you use a single equal sign in assigning variable names. Think of that as meaning is. But, if you want to make the state-ment that something is equal to in JavaScript, you use the double equal signs.
Yes, we know it sounds backward, but that’s the way it is.
We keep it straight by thinking that a double (==) means is equal to, and a single (=) means is.
Back to the Deconstruction
The code then sets two more cliché numbers to var1andvar2, depending on whether 1 or 2 is chosen:
if (num == 1)
{cliche=var1}
JavaScript Goodies
164
if (num == 2)
{cliche=var2}
document.write(cliche)
Finally, a document.writestatement is used to write the cliché to the page. Because there is no text surrounding cliche, no plus signs are necessary.
What’s Happening?
The process is quite linear. First, three variable names are set, attaching three text strings to the variables var0throughvar2.
Next, a random number is chosen and written to the page.
Then, that number is looked at through a series of ifstatements. If the first statement isn’t true,numdoes not equal 0, and the next ifstatement is looked at. If that statement isn’t true, the script goes on to the next.
One of those statements will be true because only 0, 1, or 2 will be returned from the ran-dom number code.
Why Don’t You Use an
elsewith Your
if?
It isn’t necessary. One of those ifstatements will be true. You could just as easily have written this so that the last ifstatement was an else. It would have worked just the same way, but again, it’s not necessary. Three ifs will do just fine.
Your Assignment
We have three pictures for you at
http://www.htmlgoodies.com/JSBook/pic1.gif http://www.htmlgoodies.com/JSBook/pic2.gif http://www.htmlgoodies.com/JSBook/pic3.gif
Modify this JavaScript program to display a random picture rather than text. Make the text under the image read describes my mood today.
You can see a possible answer to this assignment on your own computer by clicking Lesson Thirty-Four Assignment in your download packet, or see it online at http://www.
htmlgoodies.com/JSBook/assignment34.html.