Challenge

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

▶ Do the exercises

Exercise 1 — Codédex Entry Formatter

The Codédex prints one neat line per Codémon, with an identifier, a padded name and a short code.

Step 1: `entryId(number)` returns the number on 3 digits filled with zeros, prefixed by `"#"`. For `7` it returns `"#007"`. Step 2: `shortCode(name)` returns the first three letters of the name in uppercase. Step 3: for each Codémon, print its id, then its name padded to 10 characters with spaces, then its short code. Step 4: close with a line of 20 dashes built with `.repeat()`.

Expected output:

#007 Aquachu AQU #042 Zap ZAP #150 Metachu MET --------------------
Starter code
const codedex = [ { number: 7, name: "Aquachu" }, { number: 42, name: "Zap" }, { number: 150, name: "Metachu" } ]; // Write your functions here // Use them here

Exercise 2 — Log File Cleaner

A log file arrives messy: some lines aren't logs at all, and the real ones use underscores instead of spaces.

Step 1: keep only the lines that start with `"LOG:"`, and print `"Kept: N"` with how many there are. Step 2: for each kept line, remove the `"LOG:"` prefix, turn every underscore into a space, and print the result. Step 3: print `"Ignored: N"` with the number of lines that were dropped.

Expected output:

Kept: 3 Zap enters the arena Aquachu uses Splash Zap wins the battle Ignored: 2
Starter code
const lines = [ "LOG:Zap_enters_the_arena", "debug: ignore me", "LOG:Aquachu_uses_Splash", "note: nothing here", "LOG:Zap_wins_the_battle" ]; // Write your code here

Exercise 3 — Health Bar Display

Draw the whole team's health bars so that every gauge and every number lines up in a column.

Step 1: `bar(hp, max)` returns a gauge where each block stands for 10 HP: `"#"` for the filled part and `"-"` for the rest, wrapped in square brackets. Step 2: for each Codémon, print its name padded to 10 characters, then its gauge, then its HP padded to 3 digits with zeros, a slash, and its max. Step 3: print `"Fainted: N"` with the number of Codémon at 0 HP.

Expected output:

Aquachu [####-] 040/50 Zap [##---] 020/50 Pyrochu [-----] 000/50 Fainted: 1
Starter code
const team = [ { name: "Aquachu", hp: 40, max: 50 }, { name: "Zap", hp: 20, max: 50 }, { name: "Pyrochu", hp: 0, max: 50 } ]; // Write your function here // Use it here
Back to the course