Pew Pew Laser Blog

Code. Glass art. Games. Baking. Cats. From Seattle, Washington and various sundry satellite locations.

JavaScript - Random Quote

Last Update: 4.27.2009

Introduction

A friend asked me to write a JavaScript to randomly quotes from the movie Jaws. So I whipped up this little script up using an array and the Math library. Careful coding also ensures that the classy blockquote CSS shows through.


Demonstration

Your random quote is:

Go ahead and refresh the page to see a new quote. You know you want to.


Code

Here's the JavaScript function:

function show_quote() {

  //  If adding more quotes, add more lines, increasing the number in the [] by 1 each time.
  var quotes = new Array();
  quotes[0]="\"We're gonna need a bigger boat.\"";
  quotes[1]="\"Not with three in him.  Not with three he can't!\"";
  quotes[2]="\"Can you do that?  
I can do anything. I'm the chief of police.\""; // Getting a random number // If adding more quotes, change the number at the end of the next line to the total number of elements in your array. var r_number = Math.floor( Math.random()*3 ); // Prints the selected random element from the array. document.write (quotes[r_number]); }

Looking for more?

Back to Pew Pew Laser Articles

Contact Me