Javascript · Conditional Statements
else if chains
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — More Than Two Choices
if
/
else
gives you two paths. But what if there are three, four, five possibilities? A Codémon can be in great shape, tired, or knocked out. That's what
else if
is for.
let florachuHP = 45; if (florachuHP > 70) { console.log("Florachu is in great shape!"); } else if (florachuHP > 20) { console.log("Florachu is tired..."); } else { console.log("Florachu is knocked out!"); }
With 45 HP: the first condition is
false
, the second is
true
, so "tired" is printed. Try
90
and
10
to see the other branches!
As Many as You Need
You can chain as many
else if
as you want between the first
if
and the final
else
. The final
else
is optional.
Exercise 2 — The First True Condition Wins
JavaScript reads the chain from top to bottom and stops at the first condition that is
true
. All the following branches are ignored, even if their conditions are also true!
let zapLevel = 42; // Both conditions are true for 42... if (zapLevel > 10) { console.log("Zap is past the beginner stage"); } else if (zapLevel > 40) { console.log("Zap is an expert!"); }
Even though
zapLevel > 40
is true, you never see "expert": the chain stopped at the first true condition. That's why order matters: put the most specific conditions first.
let level = 42; // Correct order: most specific first if (level > 40) { console.log("Expert!"); } else if (level > 10) { console.log("Intermediate"); } else { console.log("Beginner"); }
Exercise 3 — Follow the Chain
What will this code print?
let badges = 5; if (badges >= 8) { console.log("Champion"); } else if (badges >= 4) { console.log("Veteran"); } else if (badges >= 1) { console.log("Rookie"); } else { console.log("Newcomer"); }
Options
• ```Veteran```
• ```Rookie```
• ```Champion```
• ```Veteran``` and ```Rookie```
Hint : Go down the chain from the top and stop at the first condition that is true for 5. Only one branch ever runs.
Exercise 4 — Build the Chain
Complete the weather report for the Codémon arena.
let temperature = 3; if (temperature > 25) { console.log("Perfect for Pyrochu!"); } ____ ____ (temperature > 10) { console.log("Nice training weather"); } ____ { console.log("Zap should stay inside!"); }
Options: else if else elif elseif when
Hint : In JavaScript, the middle branch is written as two separate keywords. Python's ```elif``` doesn't exist here!
Exercise 5 — Order Trap
This trainer wrote their rank system in the wrong order. What does it print?
let victories = 95; if (victories >= 10) { console.log("Bronze"); } else if (victories >= 50) { console.log("Silver"); } else if (victories >= 90) { console.log("Gold"); }
Options
• ```Bronze```
• ```Gold```
• ```Silver```
• All three ranks
Hint : 95 makes all three conditions true. Which one does JavaScript reach first when reading from the top?
Exercise 6 — Type Effectiveness
Aquachu attacks! Complete the chain: double damage against
"fire"
, half damage against
"plant"
, normal damage otherwise.
let opponentType = "plant"; let damage = 50; if (opponentType === "fire") { console.log(damage * 2); } else if (opponentType ____ ____) { console.log(damage ____ 2); } else { console.log(damage); }
Options: === "plant" / == "fire" *
Hint : Use strict comparison against the right type, and think about which math operator cuts a value in half.
Exercise 7 — Which Branch for Which Value?
For this chain, match each energy value with the printed message.
if (energy >= 80) { console.log("Special attack ready"); } else if (energy >= 40) { console.log("Normal attack"); } else if (energy > 0) { console.log("Defend"); } else { console.log("Rest"); }
Items to match
• energy = 100
• energy = 79
• energy = 39
• energy = 0
Definitions
• Special attack ready
• Normal attack
• Defend
• Rest
Exercise 8 — Chain Without a Final else
What will this code print?
let rockachuWeight = 30; if (rockachuWeight > 100) { console.log("Heavyweight"); } else if (rockachuWeight > 50) { console.log("Middleweight"); } console.log("Weighing done");
Options
• Only ```Weighing done```
• ```Middleweight``` then ```Weighing done```
• An error, ```else``` is missing
• ```Weighing done``` ```Middleweight```
Hint : The final ```else``` is optional. If no condition in the chain is true, the whole chain simply does nothing.
Exercise 9 — The Rank Machine
Build the official trainer rank machine! Depending on
victories
, print exactly one rank: 1. 100 or more:
"Legend"
2. 50 to 99:
"Gold"
3. 20 to 49:
"Silver"
4. below 20:
"Bronze"
Careful with the order of your conditions!
Starter code
let victories = 64; // Write your chain here
Back to the course