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, ...

Get JavaScript™ 1.5 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.