CODE BUILDERS Lesson 5 of 6 ← All courses
Station 5 · Gadget Lab

Make It Interactive

Time for the moves! Today you'll use JavaScript to make buttons that actually do things, and get better at fixing bugs.

About 45 minutes. You can stop and come back. Your answers are saved in this browser.

Part 1 · Warm-up

What do you remember?

Last time your site grew to several pages. Quick warm-up!

Part 2 · Learn it

When this happens, do that

JavaScript makes your page react. Most of it follows one pattern: "When something happens, do something." The "something happens" part is called an event, like a click.

JavaScript goes inside a <script> tag, usually at the bottom of the <body>. It finds parts of the page by their id, a unique name you give an element.

Watch it work

EXAMPLE 1A "Surprise me!" fact button, line by line
<button id="fact-btn">Surprise me!</button>
<p id="fact"></p>

<script>
  const facts = ["Octopuses have three hearts.", "Honey never goes bad.", "Sloths can hold their breath for 40 minutes."];

  document.getElementById("fact-btn").addEventListener("click", function () {
    const pick = Math.floor(Math.random() * facts.length);
    document.getElementById("fact").textContent = facts[pick];
  });
</script>
  1. The button and paragraph each get an id, so the script can find them.
  2. const facts = [...] is a list (called an array) of facts. Each one is in quotes.
  3. addEventListener("click", ...) means "when the button is clicked, run the code inside."
  4. Math.random() picks a random number, so pick is a random position in the list.
  5. .textContent = facts[pick] puts that fact into the paragraph.

You'll finish a machine just like this in the playground!

EXAMPLE 2Show the answer
<p>Which planet is closest to the Sun?</p>
<button id="show-btn">Show answer</button>
<p id="answer" hidden>Mercury!</p>

<script>
  document.getElementById("show-btn").addEventListener("click", function () {
    document.getElementById("answer").hidden = false;
  });
</script>

hidden hides the answer. When you click, hidden = false shows it. That's a mini quiz!

EXAMPLE 3Reporting a bug clearly
YouIt doesn't work.
AII'm sorry to hear that! Could you tell me more about what isn't working?

The AI can't see your screen, so it has to guess. Now a clear bug report:

YouMy "Surprise me!" button doesn't do anything. I expected a fact to appear when I click it. What happens: nothing at all. Here is my code: [pasted the button and the script]. Please explain the problem, don't just fix it.
AIFound it! Your button has id="fact-btn", but your script looks for "factBtn". The names must match exactly. Change one of them so they're the same.

A great bug report has three parts: what you expected, what actually happened and the code. Then test the fix to make sure it really works.

🔍 Code Check for JavaScript

  1. Does any script load from another website (src="https://...")?
  2. Does it ask for information or send anything away (look for prompt( or fetch()?
  3. Does it make pop-ups that keep coming back?
  4. Is there any line you don't understand? Ask the AI to explain it before you run it.
Byte says

Scripts are fussy! One missing quote or bracket, or one wrong capital letter, and nothing works. That's normal. Even professional coders spend lots of time hunting for tiny bugs.

Part 3 · Spot it

Bug Hunt

Each script has one bug that stops it from working. Find the line with the bug!

Bug hunter! 🐞

Part 4 · Build it

Finish the Fact Machine

The fact machine is almost done. Write the missing line where it says YOUR TURN, then add a fact of your own and click the button in the preview to test it!

📄 index.html
Live preview

    The playground only runs inside this page. It can't open other websites or change anything on your computer. Your code is saved in this browser, on this device.

    Part 5 · Try it for real

    Gadget Lab Missions

    Time to use your own chatbot. Missions get harder as you go. Bronze is the one to do today. Silver and Gold are extra challenges for when you want more.

    Test scripts with a grown-up nearby

    JavaScript makes things happen, so Code Check every script before you open the page. Test new gadgets with a grown-up nearby, and never run a script you don't understand.

    ★ Bronze

    Surprise Button

    Add a random fact (or joke) button to your site.

    1. Add a button, an empty paragraph and a script like Example 1 to one of your pages. Use facts about your topic!
    2. Give the button and paragraph ids, and check the script uses exactly the same ids.
    3. Save, refresh and click it lots of times. Do all the facts appear?
    ★★ SilverExtra challenge

    Mini Quiz

    Add a 3-question quiz about your topic.

    1. Use Example 2: a question, a "Show answer" button and a hidden answer.
    2. Make 3 questions. Each button and answer needs its own id (like show-1, answer-1).
    3. Test every button. If one doesn't work, write a clear bug report to your AI tool.
    ★★★ GoldExtra challenge

    Your Own Gadget

    Design a gadget of your own.

    1. Pick an idea: a click counter, a light/dark switch, a "reveal the secret" card, or your own idea.
    2. Ask your AI tool for help with a recipe prompt. Code Check the answer.
    3. Before you add it, explain every line to yourself (or to someone at home). If you can't explain a line, ask the AI about it.

    Your notes are saved right in this browser, on this device. They're never sent anywhere.

    Part 6 · Build Log

    Add a page to your Build Log

    Add today's page to your Build Log.

    🛠️ Make It Interactive

    PAGE 5 OF 6

    Your Build Log is saved right in this browser, on this device. It's never sent anywhere.

    Recap

    What you just learned

    Part 7 · Show what you know

    Quick Quiz

    10 questions, picked at random from a bigger question bank. If you take it again, you'll probably see a different mix. Let's go!

    Up next

    Lesson 6 · Launch Day

    Finish, test and show off your website.

    Go to Lesson 6 →