Challenge 1: Data

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

▶ Do the exercises

Exercise 1 — The Codédex Report

Welcome to your first final challenge! A trainer's save file arrives as raw JSON text. Turn it into a clean, aligned report — and survive a corrupted file along the way. This challenge combines JSON, destructuring, array methods, string padding, `Set` and error handling. Take your time and build it step by step.

Step 1: `loadSave(text)` parses the JSON text inside a `try...catch` and returns the resulting object, or `null` if the text is invalid. Step 2: `printReport(text)` loads the save with `loadSave`. If the result is `null`, it prints `"Corrupted save file"` and stops. Otherwise it prints the header `"=== Sam's Codédex (5 badges) ==="` using the trainer's name and badges. Step 3: for each team member, print one line: the name padded to 10 characters, then `"Lv.X"` padded to 6, then `"hp/maxHp"`. Add `" FAINTED"` at the end when the hp is 0. Step 4: then print three stats lines: - `"Ready to fight: N/M"` — members with hp above 0, out of the team size - `"Average level: X"` — computed with `reduce` - `"Unique moves: N"` — the number of different moves across the whole team (use a `Set`) Step 5: call `printReport(saveFile)`, then `printReport(brokenFile)`.

Expected output:

=== Sam's Codédex (5 badges) === Zap Lv.12 40/40 Aquachu Lv.15 0/55 FAINTED Pyrochu Lv.9 20/32 Florachu Lv.15 48/48 Rockachu Lv.7 15/30 Ready to fight: 4/5 Average level: 11.6 Unique moves: 7 Corrupted save file
Starter code
const saveFile = '{"trainer":{"name":"Sam","badges":5},"team":[{"name":"Zap","level":12,"hp":40,"maxHp":40,"moves":["Tackle","Spark","Thunder"]},{"name":"Aquachu","level":15,"hp":0,"maxHp":55,"moves":["Tackle","Bubble"]},{"name":"Pyrochu","level":9,"hp":20,"maxHp":32,"moves":["Tackle","Ember"]},{"name":"Florachu","level":15,"hp":48,"maxHp":48,"moves":["Vine Whip","Tackle"]},{"name":"Rockachu","level":7,"hp":15,"maxHp":30,"moves":["Rock Throw"]}]}'; const brokenFile = "{trainer: Sam}"; // Write loadSave and printReport here // Print the two reports here
Back to the course