Challenge

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

▶ Do the exercises

Exercise 1 — Season Statistics

The season is over. Produce the full statistics report for the roster.

Step 1: with `reduce`, compute `totalWins`, the sum of every fighter's wins, and print `"Total wins: N"`. Step 2: print `"Average wins: X"`, the total divided by the number of fighters. Step 3: print `"Everyone won once: X"` where X says whether every fighter has at least 1 win, and `"Someone is undefeated: X"` where X says whether at least one has 0 losses. Step 4: build `ranked`, a copy sorted by wins from highest to lowest, and print one line per fighter as `"N. name (W wins)"`, numbered from 1. The original `roster` must keep its order, so print its first name at the end as `"Roster first: X"`.

Expected output:

Total wins: 12 Average wins: 4 Everyone won once: true Someone is undefeated: true 1. Metachu (7 wins) 2. Aquachu (4 wins) 3. Zap (1 wins) Roster first: Zap
Starter code
const roster = [ { name: "Zap", wins: 1, losses: 5 }, { name: "Metachu", wins: 7, losses: 0 }, { name: "Aquachu", wins: 4, losses: 2 } ]; // Write your code here

Exercise 2 — Save File Pipeline

Three save files arrive as lines of text. Turn them into one clean, readable roster.

Step 1: each line holds names separated by `;`. Build `all`, a single flat list of every name from every line, and print `"Loaded: N"`. Step 2: print the names in uppercase, joined with `" | "`. Step 3: build `sorted`, a copy of `all` in alphabetical order, and print it joined with `", "`. Step 4: print `"Short names: N"` with the number of names of 3 characters or fewer.

Expected output:

Loaded: 5 AQUACHU | ZAP | PYROCHU | METACHU | RHEA Aquachu, Metachu, Pyrochu, Rhea, Zap Short names: 1
Starter code
const lines = ["Aquachu;Zap", "Pyrochu", "Metachu;Rhea"]; // Write your code here

Exercise 3 — Build the Arena Schedule

The arena runs one match per day for a week. Generate the schedule from nothing, then report on it.

Step 1: build `days`, the numbers 1 to 7. Step 2: build `schedule`, one object per day with a `day` property and a `fee` property, where the fee is the day number times 20. Step 3: print one line per day, in the format `"Day X: Y gold"`. Step 4: with `reduce`, print `"Total fees: N"`. Then print `"Free days: N"` with the number of days whose fee is below 50, and `"Priciest day: X"` with the day number of the highest fee, found without modifying `schedule`.

Expected output:

Day 1: 20 gold Day 2: 40 gold Day 3: 60 gold Day 4: 80 gold Day 5: 100 gold Day 6: 120 gold Day 7: 140 gold Total fees: 560 Free days: 2 Priciest day: 7
Starter code
// Write your code here
Back to the course