Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

JavaScript: Random Quotations - Generating Quotes Randomly on a Webpage, Exercises of Web Programming and Technologies

How to write a simple javascript code to display a randomly selected quote from a list on a webpage. It covers creating an array of quotes, selecting a random integer, and writing the quote to the html document.

Typology: Exercises

2011/2012

Uploaded on 07/18/2012

palavii
palavii 🇮🇳

5

(5)

51 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COMP 519 Web Programming
JavaScript: Random quotations
How can you write a simple JavaScript to display a randomly selected quote
on a webpage? This will be from a list that you specify.
Probably the first thing to do in the script is to declare a new array variable
like:
var quotes = new Array();
Then you can load the array with quotes of your choice. You should insert
them sequentially so that there will be no gaps in the array (otherwise the array
value will be undefined if you were to try to print it out). Note that you should
enter the quotes as a single line, but for space reasons they’re given over multiple
lines here.
quotes[0] = "Hey, you can’t fight in here! This is the war room!
<br /> - Dr. Strangelove";
quotes[1] = "He who fights fire with fire burns his house down
twice as fast. <br /> - Vietnamese proverb";
quotes[2] = "I have not yet begun to fight. <br />
- John Paul Jones";
Now having entered your quotes into an array, you need some way of selecting
one of them. You can use the Math.random() function to help with this. This
function gives a random real number xwhere 0 x < 1. How can you translate
this in some fashion into an integer between 0 and quote.length-1 (inclusive),
recalling that arrays are indexed beginning at 0? (This is something that you
should think about. It’s not difficult, but I don’t want to give away everything
here.) Let’s assume we have a function randomInteger(low, high) that gives
you an integer between low and high (inclusive).
Then having selected a random integer, you can use the document.write
command to write it to the HTML document.
Easy, huh?
You can insert your code into any document, or you can save it to a separate
file and insert it in your HTML code in the manner I described in class.
A fully working JavaScript function is shown on the next page.
1
docsity.com
pf2

Partial preview of the text

Download JavaScript: Random Quotations - Generating Quotes Randomly on a Webpage and more Exercises Web Programming and Technologies in PDF only on Docsity!

COMP 519 Web Programming

JavaScript: Random quotations

How can you write a simple JavaScript to display a randomly selected quote on a webpage? This will be from a list that you specify. Probably the first thing to do in the script is to declare a new array variable like:

var quotes = new Array(); Then you can load the array with quotes of your choice. You should insert them sequentially so that there will be no gaps in the array (otherwise the array value will be undefined if you were to try to print it out). Note that you should enter the quotes as a single line, but for space reasons they’re given over multiple lines here.

quotes[0] = "Hey, you can’t fight in here! This is the war room!
- Dr. Strangelove"; quotes[1] = "He who fights fire with fire burns his house down twice as fast.
- Vietnamese proverb"; quotes[2] = "I have not yet begun to fight.

  • John Paul Jones"; Now having entered your quotes into an array, you need some way of selecting one of them. You can use the Math.random() function to help with this. This function gives a random real number x where 0 ≤ x < 1. How can you translate this in some fashion into an integer between 0 and quote.length-1 (inclusive), recalling that arrays are indexed beginning at 0? (This is something that you should think about. It’s not difficult, but I don’t want to give away everything here.) Let’s assume we have a function randomInteger(low, high) that gives you an integer between low and high (inclusive).

Then having selected a random integer, you can use the document.write command to write it to the HTML document.

Easy, huh? You can insert your code into any document, or you can save it to a separate file and insert it in your HTML code in the manner I described in class. A fully working JavaScript function is shown on the next page.

docsity.com

docsity.com