Lesson 9 of 11
Choose Your Own Adventure
A story where you click to choose. JavaScript makes every paragraph and every button.
What you will learn
- Functions with parameters
- Helper functions
- createElement
- appendChild
- Passing a function to a listener
Before you start
Calculator. In that lesson your code reacted to the page. In this lesson your code builds the page.
What this page does
Read the text and click a button to choose. New text and new buttons appear. This keeps going until you reach an ending. Open adventure.html and look for the story. It is not there. JavaScript made every word.
How it works
Two small helper functions do all the work.
createParagraph(text)makes a<p>, puts the text in it, and adds it to the page.createButton(text, onClick)makes a<button>and labels it. It connects a function to the button's click. Then it adds the button to the page.
Each choice is a function, such as goLeft or petSquirrel. Each one calls the two helper functions. The helper functions add the next part of the story and the next set of buttons. startAdventure() runs when the page loads.
Build it step by step
Build the two helper functions first. Then grow the story one branch at a time.
Make the page
Make
adventure.htmlwith a heading and an intro paragraph. Put the<script>tag at the end of<body>. Open the page. You see the intro paragraph and nothing else.Write the paragraph helper
Write
createParagraph(text). Then call it once at the bottom of the file withcreateParagraph("test");. Check that the text appears on the page.function createParagraph(text) { var newParagraph = document.createElement("p"); // make a <p> newParagraph.textContent = text; // put text in it document.body.appendChild(newParagraph); // add it to the page }Write the button helper
Write
createButton(text, onClick). It follows the same pattern. It also adds a click listener. Test it. Clicking should add a paragraph.function createButton(text, onClick) { var newButton = document.createElement("button"); // make a <button> newButton.textContent = text; // label it newButton.addEventListener("click", onClick); // run onClick on click document.body.appendChild(newButton); // add it to the page }Write the first scene
Delete your test calls. Write
startAdventure(). It uses both helper functions to set up the first choice.goLeftandgoRightdo not exist yet. Write them as empty functions for now.function startAdventure() { createParagraph("You are in a dark forest. Go left or right?"); createButton("Left", goLeft); createButton("Right", goRight); }Grow each branch
Fill in one choice function at a time. Each one follows the same pattern. It makes one paragraph for the story. Then it makes one button for each choice. An ending is a function with a paragraph and no buttons.
function goRight() { createParagraph("You fall into a pit. Game over."); }Start the game
Call
startAdventure()on the last line of the file. The first scene now appears when the page opens.
Try these changes
Make these changes in the files beside this guide. Each one only uses what this lesson has already covered. Tick each one when it works.
Check yourself
Three quick questions on this lesson. Get them all right to finish the lesson and earn a pack.
Loading your answers
Teacher's versionHow to run this lesson, what to check, and any answers. Students do not need this.
How to teach it
- Test each helper function on its own before any story exists. A student who trusts the two helper functions can write the rest alone.
- The missing brackets in
createButton("Left", goLeft)are the bug of this lesson. WritegoLeft()on purpose and reload the page. The page runs the scene right away. Then fix it. - Plan the story on paper as a tree before coding. Draw one box per scene. Draw one arrow per choice. Writing the code is then easy.
- The empty functions in step 4 stop the page showing an error. Explain that this is normal practice, not a trick.