If / Then Builder
Students change a boolean variable from true to false and test the result.
Conditions can ask about numbers
So far the guard only checked a yes/no box. Now the guard can do math!
A comparison like playerScore >= 10 asks a question
about a number — "is the score at least 10?" — and JavaScript answers
true or false all by itself.
-
1
A comparison makes a boolean
let hasEnoughPoints = playerScore >= 10;opens theplayerScorebox, finds 12, checks "is 12 at least 10?" and puts the answer —true— into the new box. You never typed true; the program figured it out. -
2
The comparison symbols
>means greater than,<means less than,>=means at least (greater or equal), and<=means at most. The alligator trick still works: the open mouth always chomps the bigger number. -
3
Change the number, change the choice
Run the example: with a score of 12, the rainbow shield unlocks. Now change
12to7and run again — the score still prints, but the shield stays locked because 7 isn't at least 10. This is exactly how real games decide when you've earned the next level.
Change the choice.
let playerScore = 12;
let hasEnoughPoints = playerScore >= 10;
say("Score: " + playerScore);
if (hasEnoughPoints) {
say("Unlock the rainbow shield.");
}
Program result
- Run your code to see output.
Type real JavaScript, then run it.
You tested a condition that changes when the variable changes.
Try If / Then Challenge