1. Build this in Design Mode first
STEP 1Create these elements on screen1 with these exact IDs and starting text.
| Type | ID | Text |
|---|---|---|
| Label | speedLabel | Speed: -- |
| Label | statusLabel | Status: -- |
| Button | btn10 | Speed: 10 |
| Button | btn25 | Speed: 25 |
| Button | btn26 | Speed: 26 |
| Button | btn40 | Speed: 40 |
2. Switch to Code Mode
STEP 2Copy this starter code into Code Mode. Three of the four button handlers already echo their speed into speedLabel — your job is the one if-statement, built once inside btn25's handler, then copied into the other three.
Before you type anything, say the rule out loud: 25 mph or under is OK; anything over that is Over the limit.
// SETUP (already works — do not edit)
onEvent("btn10", "click", function() {
var speed = 10;
setText("speedLabel", "Speed: " + speed);
// (fill in after btn25 works)
});
onEvent("btn26", "click", function() {
var speed = 26;
setText("speedLabel", "Speed: " + speed);
// (fill in after btn25 works)
});
onEvent("btn40", "click", function() {
var speed = 40;
setText("speedLabel", "Speed: " + speed);
// (fill in after btn25 works)
});
// STEP 1: Build this one first. Write ONE if-statement that sets
// statusLabel:
// speed <= 25 -> "Status: OK"
// otherwise -> "Status: Over the limit"
// A single condition with two outcomes — not a chain.
onEvent("btn25", "click", function() {
var speed = 25;
setText("speedLabel", "Speed: " + speed);
});
3. Copy the pattern into the other three buttons
STEP 3Once btn25 works, copy the exact same if-statement into btn10, btn26, and btn40's handlers. Same repetition idea as the demo — you'll avoid this retyping later once you learn functions.
4. Check yourself — this is the actual lesson
STEP 4Click each button and predict the status before you click. The four speeds are chosen on purpose to sit right on the one boundary:
| Button | What it should test |
|---|---|
| 10 | clearly OK |
| 25 | exactly the cutoff — does your <= actually include it? |
| 26 | one over the cutoff |
| 40 | clearly over the limit |
If 25 and 26 give the same status, that's a boundary bug — re-read your comparison operator (<, <=, >=, >) before assuming the condition is wrong.
Try it for real before checking the answer key.
Answer key
onEvent("btn10", "click", function() {
var speed = 10;
setText("speedLabel", "Speed: " + speed);
if (speed <= 25) {
setText("statusLabel", "Status: OK");
} else {
setText("statusLabel", "Status: Over the limit");
}
});
onEvent("btn25", "click", function() {
var speed = 25;
setText("speedLabel", "Speed: " + speed);
if (speed <= 25) {
setText("statusLabel", "Status: OK");
} else {
setText("statusLabel", "Status: Over the limit");
}
});
onEvent("btn26", "click", function() {
var speed = 26;
setText("speedLabel", "Speed: " + speed);
if (speed <= 25) {
setText("statusLabel", "Status: OK");
} else {
setText("statusLabel", "Status: Over the limit");
}
});
onEvent("btn40", "click", function() {
var speed = 40;
setText("speedLabel", "Speed: " + speed);
if (speed <= 25) {
setText("statusLabel", "Status: OK");
} else {
setText("statusLabel", "Status: Over the limit");
}
});