Finding the Maximum or Minimum Value
We can use the
floor() methods to create a random quotes script. Place this in an external file with a link to it in the
head section of the page where you want the quotes displayed:
Then place this in the place on the page where you want the quotes to be displayed:
Basically, what we did here was to create an array and give it six elements. We then set a formula to create a "random number generator." First we declared the variable
quote, and gave it our formula. The "formula" goes like this: we used the
floor() method to return an integer; then we created a random number by using the
random() method and multiplying it by 6 (the number of elements in our array). The
document.write object then displays the random quote.
You can do the same thing for images. You can use the script above, changing the quotes to images, or use one like below:
Then, just place this where you want the image displayed on the page:
This one is a bit different than the previous one. It uses the
getElementById() method to replace the contents of the
If you plan on using several statements with an object, you can use the
with statement as a shortcut. For instance, to calculate the area of a circle, you would use the following statement:
With this format, you need to write the Math object each time you want to call a property or method. Instead, you can use the
Another example (with thanks to William Wen) that demonstrates it a little better is as follows. This script:
could be written as:
That should save some time when you have to reuse the same object over and over.
That wraps up our study of the
In our next installment, we'll take a look at the
Date object and its different uses. Until then, keep on codin'!