Challenge
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Battle Damage Calculator
Pyrochu attacks! Compute the real damage based on the opponent's type:
1. Against "plant": damage is doubled (super effective!)
2. Against "water": damage is divided by 2 (not very effective...)
3. Against "fire": damage is 0 (immune!)
4. Any other type: normal damage
Store the result in finalDamage and print it.
let opponentType = "plant";
let baseDamage = 30;
let finalDamage;
// Write your conditions here
console.log("Damage dealt: " + finalDamage);Exercise 2 — The Potion Shop
You enter the potion shop with your coins.
1. Use a ternary to set price: 80 if potionSize is "large", otherwise 30
2. With an if/else, check if coins is enough to pay price:
- if yes, print "Purchase successful!" and the remaining coins
- if no, print "Not enough coins..." and how many are missing
let potionSize = "large";
let coins = 65;
// 1. Ternary for the price
let price =
// 2. Can you afford it?Exercise 3 — The Arena Master
Final boss! Combine everything:
1. With a switch on chosenCodemon, set attackPower:
- "pyrochu": 45
- "aquachu": 40
- "zap": 50
- any other: 25
2. Then with an if/else if/else, print the verdict:
- attackPower 50 or more: "Crushing victory!"
- 40 or more: "Close victory!"
- below 40: "Defeat..."
let chosenCodemon = "zap";
let attackPower;
// 1. switch to set attackPower
// 2. if/else if/else for the verdict