? Week 4 Day 1 Challenge — Traffic Light Simulator
CodeX Academy · Level 1 — App Lab · Lessons 1-2

Traffic Light Simulator

Optional extra practice for Lessons 1–2 (Design Mode, IDs, events). Same skill as tonight's demo (control panel) — a different scenario, on your own.

1. Build this in Design Mode first

STEP 1

Create these elements on screen1 with these exact IDs and starting text.

TypeIDText
LabellightLabelSTOP
ButtonredBtnRed
ButtonyellowBtnYellow
ButtongreenBtnGreen

Bonus only — add this after Step 3 works:

TypeIDText
ButtonresetBtnReset

2. Switch to Code Mode

STEP 2

Copy this starter code into Code Mode and fill in the numbered STEPs.

// STEP 1: When redBtn is clicked, set lightLabel's text to "STOP" and its
// background color to "red".


// STEP 2: When yellowBtn is clicked, set lightLabel's text to "SLOW DOWN"
// and its background color to "yellow".


// STEP 3: When greenBtn is clicked, set lightLabel's text to "GO" and its
// background color to "green".


// ------------------------------------------------------
// BONUS CHALLENGE 🌟
// First, add resetBtn in Design Mode (see the table above).
// ------------------------------------------------------

// BONUS STEP 4: When resetBtn is clicked, set lightLabel back to "STOP"
// with a red background — the same as the app's starting state.

3. Check yourself

STEP 3
  • Click each button. Does lightLabel show the right word AND the right background color every time?
  • Click the same button twice in a row — nothing should break.
  • Try it for real before checking the answer key.
Answer key
// STEP 1
onEvent("redBtn", "click", function() {
  setText("lightLabel", "STOP");
  setProperty("lightLabel", "background-color", "red");
});

// STEP 2
onEvent("yellowBtn", "click", function() {
  setText("lightLabel", "SLOW DOWN");
  setProperty("lightLabel", "background-color", "yellow");
});

// STEP 3
onEvent("greenBtn", "click", function() {
  setText("lightLabel", "GO");
  setProperty("lightLabel", "background-color", "green");
});

// BONUS STEP 4
onEvent("resetBtn", "click", function() {
  setText("lightLabel", "STOP");
  setProperty("lightLabel", "background-color", "red");
});