O'Reilly logo

JavaScript™ 1.5 by Example by Kathie Kingsley-Hughes, Adrian Kingsley-Hughes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exercise—Interactive Story

Let's work through an example that uses arrays to create an interactive story. You might be familiar with this type of “adlib” game in which the user is asked for a series of words—for example, adjectives, nouns, and so on. The user's answers are then inserted into a well-known piece of text to make a totally unique and hopefully amusing story. Here's how to create your own game:

1.
Choose the story/rhyme/lyrics you want to use, pick out the words you want to replace, and give them each a unique variable name:
 Once upon a entry1 dreary, while I pondered, entry2 and weary, Over many a entry3 and curious volume of entry4 lore-- While I entry5, nearly napping, entry6 there came a tapping, As of some one entry7 rapping, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required