Challenge

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

▶ Do the exercises

Exercise 1 — Gym Leaderboard

The gym needs its leaderboard. Build it from the list of fighters, using array methods only.

Step 1: print one line per fighter, in the format `"name: W wins / L losses"`. Step 2: build `winners`, the names of the fighters with more wins than losses, and print `"Winners: N"` with how many there are. Step 3: print each winner's name on its own line with `"* "` in front. Step 4: print `"Total wins: N"` with the sum of every fighter's wins.

Expected output:

Aquachu: 5 wins / 2 losses Pyrochu: 1 wins / 4 losses Zap: 3 wins / 3 losses Metachu: 7 wins / 1 losses Winners: 2 * Aquachu * Metachu Total wins: 16
Starter code
const fighters = [ { name: "Aquachu", wins: 5, losses: 2 }, { name: "Pyrochu", wins: 1, losses: 4 }, { name: "Zap", wins: 3, losses: 3 }, { name: "Metachu", wins: 7, losses: 1 } ]; // Write your code here

Exercise 2 — Incomplete Save Files

The game must load save files written by three different versions of itself. Read them all without a single crash.

Step 1: `summarize(save)` returns `"name: N Codémon, starter X, Y badges"`. Missing name becomes `"unknown"`, missing team counts as 0, missing starter becomes `"none"`, missing badges count as 0. Step 2: print the summary of every save. Step 3: print `"Complete saves: N"` with the number of saves that have both a team and a starter.

Expected output:

Sam: 2 Codémon, starter Aquachu, 3 badges Lea: 0 Codémon, starter none, 0 badges unknown: 1 Codémon, starter Metachu, 0 badges Complete saves: 2
Starter code
const saves = [ { name: "Sam", team: ["Aquachu", "Zap"], starter: { name: "Aquachu" }, badges: ["Rock", "Water", "Fire"] }, { name: "Lea" }, { team: ["Metachu"], starter: { name: "Metachu" } } ]; // Write your function here // Use it here

Exercise 3 — Team Move Database

Walk two levels of nested data to build a report on the whole roster's moves.

Step 1: print one line per Codémon, in the format `"name knows N move(s)"`. Step 2: print `"Specialists: N"` with the number of Codémon knowing 2 moves or more. Step 3: print `"Total moves: N"` with the sum of all the moves of the roster. Step 4: `knowsMove(name)` returns `true` if any Codémon knows that move. Print the result for `"Spark"` and for `"Fly"`.

Expected output:

Aquachu knows 2 move(s) Zap knows 3 move(s) Pyrochu knows 1 move(s) Specialists: 2 Total moves: 6 Spark: true Fly: false
Starter code
const roster = [ { name: "Aquachu", moves: ["Splash", "Wave"] }, { name: "Zap", moves: ["Spark", "Thunder", "Charge"] }, { name: "Pyrochu", moves: ["Flame"] } ]; // Write your code here
Back to the course