Challenge

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

▶ Do the exercises

Exercise 1 — Unique Move Pool

Several Codémon share the same moves. Build the arena's move pool, where each move appears only once.

Step 1: gather every move of every Codémon into one flat array, and print `"Moves listed: N"`. Step 2: build a Set from that array and print `"Different moves: N"`. Step 3: turn the Set back into an array, sort it alphabetically, and print the moves joined with `", "`. Step 4: print `"Shared: N"` with the number of moves that appeared more than once in the original list.

Expected output:

Moves listed: 7 Different moves: 5 Charge, Flame, Spark, Splash, Wave Shared: 2
Starter code
const roster = [ { name: "Aquachu", moves: ["Splash", "Wave"] }, { name: "Zap", moves: ["Spark", "Charge", "Splash"] }, { name: "Pyrochu", moves: ["Flame", "Charge"] } ]; // Write your code here

Exercise 2 — Encounter Counter

Count how many times each Codémon was met during a walk. A Map is the right tool: the keys are created as you discover new names.

Step 1: build an empty Map named `counts`. For each encounter, if the name is already a key, store its value plus 1. Otherwise store 1. Step 2: print `"Different Codémon: N"` with the size of the Map. Step 3: for each pair, print `"name: N time(s)"`. Step 4: print `"Most common: X"` with the name having the highest count, found by sorting a copy of the pairs.

Expected output:

Different Codémon: 3 Zap: 3 time(s) Aquachu: 2 time(s) Metachu: 1 time(s) Most common: Zap
Starter code
const encounters = ["Zap", "Aquachu", "Zap", "Metachu", "Aquachu", "Zap"]; // Write your code here

Exercise 3 — Team Registry

Run the gym's registry: a Set of the Codémon currently present, and a Map holding each one's level.

Step 1: `register(name, level)` adds the name to the Set and stores its level in the Map. If the name was already registered, it prints `"Already here: X"` and changes nothing. Step 2: register Zap at 4, Aquachu at 12, then Zap at 99 again. Step 3: print `"Present: N"` with the Set size, then each name with its level in the format `"name (Lv.X)"`. Step 4: Zap leaves: remove it from both collections. Then print `"Present: N"` and `"Zap level: X"`.

Expected output:

Already here: Zap Present: 2 Zap (Lv.4) Aquachu (Lv.12) Present: 1 Zap level: undefined
Starter code
const present = new Set(); const levels = new Map(); // Write your function here // Use it here
Back to the course