if/else structure

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 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..."); }
Options
• Still charging...
• Full power!
• Both messages
• Nothing at all

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?

Options
• 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"); }
Items to match
• hp = 80
• hp = 50
• hp = 49
• hp = 0
Definitions
• Ready to fight
• Ready to fight
• Needs healing
• Needs healing

Exercise 8 — Boolean Variable as Condition

What will this code print?

let isSleeping = false; if (isSleeping) { console.log("Zzz..."); } else { console.log("Zap is awake!"); }
Options
• Zap is awake!
• Zzz...
• An error, the condition has no comparison
• Nothing at all

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!"

Starter code
let rockachuHP = 25; let maxHP = 100; // Write your if/else here
Back to the course