Chapter 10

Creating Your Own JavaScript Word Game

Word replacement games give you a basic story structure and ask you to supply different parts of speech: nouns, verbs, adjectives, adverbs, and so on. If you think about it, these games are very similar to JavaScript web apps. The basic structure is provided by the HTML, the CSS, and the JavaScript code. The variables in the JavaScript code are what make the results of the program different every time it’s run.

In this chapter, we create a word replacement game by using the string concatenation operator to combine the words you enter with a story.

image

Creating a Variable Story

For this first version of the game, we wrote a story about Douglas the JavaScript Robot’s adventures. Then we removed some of the words and replaced them with just a part of speech, so that the person playing the game can fill them in.

Here’s our story about Douglas the JavaScript Robot, with certain words removed and replaced with the input expected (underlined):

  • One adjective day, Douglas was verb ending in “–ing” in his room in house, reading a book about color plural noun.
  • As he past-tense verb his beverage, he heard type of music music playing in the different room in house.
  • exclamation! he exclaimed, as he past-tense verb down the stairs to join the adjective party.
  • Douglas danced the name of animal Dance, the name of city Twist, and took the prize ...

Get JavaScript For Kids For Dummies 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.