if/else structure
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — The else Branch
With if alone, something happens... or nothing at all. But often you want a plan B: attack OR defend, win OR lose. That's the job of else.
let pyrochuHP = 12;
if (pyrochuHP > 30) {
console.log("Pyrochu attacks!");
} else {
console.log("Pyrochu retreats to safety!");
}The condition is false (12 is not greater than 30), so the else block runs. Change the HP to 50 and see the other branch run instead.
No Condition on else
else never has its own condition. It simply means: "in all other cases". Writing else (hp < 30) is a syntax error.
Exercise 2 — Exactly One Branch Runs
With if/else, there are only two possible paths, and exactly one of them always runs. Never both, never zero.
let aquachuLevel = 8;
let requiredLevel = 10;
if (aquachuLevel >= requiredLevel) {
console.log("Aquachu enters the tournament!");
} else {
console.log("Aquachu needs more training...");
console.log("Missing levels: " + (requiredLevel - aquachuLevel));
}
console.log("See you at the arena!");A branch can contain several lines. And the code after the whole if/else runs in every case.
Exercise 3 — Which Branch Runs?
What will this code print?
let florachuEnergy = 60;
if (florachuEnergy === 100) {
console.log("Full power!");
} else {
console.log("Still charging...");
}Still charging...Full power!Hint : === checks strict equality. Is 60 strictly equal to 100?
Exercise 4 — Win or Lose
Complete the battle result: if Pyrochu's score is higher than the opponent's, it wins. Otherwise, it loses.
Hint : One keyword starts the decision, one operator means "higher than", one keyword handles all other cases.
Exercise 5 — Spot the Error
One of these if/else structures is invalid. Which one?
else (hp < 50) { ... }if (hp < 50) { ... }if (hp < 50) { ... } else { ... }if (isReady) { ... }Hint : Only one of the two keywords is allowed to have its own condition in parentheses.
Exercise 6 — Type Advantage
Aquachu fights Pyrochu. Water beats fire! Complete the code so the attack is doubled when the opponent's type is "fire", and normal otherwise.
Hint : Use the strict comparison operator, the multiplication operator, and the keyword for "in all other cases".
Exercise 7 — Match the Situation to the Branch
For this code, match each value of hp with what gets printed.
if (hp >= 50) {
console.log("Ready to fight");
} else {
console.log("Needs healing");
}hp = 80hp = 50hp = 49hp = 0Ready to fightReady to fightNeeds healingNeeds healingExercise 8 — Boolean Variable as Condition
What will this code print?
let isSleeping = false;
if (isSleeping) {
console.log("Zzz...");
} else {
console.log("Zap is awake!");
}Zap is awake!Zzz...Hint : A boolean variable is already a valid condition. Which branch runs when the condition is false ?
Exercise 9 — The Healing Center
Welcome to the Codémon Healing Center!
1. If rockachuHP is strictly below 40, print "Rockachu needs healing!" and print the missing HP (maxHP - rockachuHP)
2. Otherwise, print "Rockachu is in great shape!"
let rockachuHP = 25;
let maxHP = 100;
// Write your if/else here