Challenge
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Shop Catalogue
The shop has a list of items with prices. Build the displays a customer sees, using array methods only.
Step 1: `labels` is an array of strings `"name: X gold"` for every item. Print each one. Step 2: `cheap` is an array of the names of items costing 50 gold or less. Print `"Cheap: "` followed by the names joined with a comma and a space (you can build that string with a loop or `forEach`). Step 3: print `"Expensive items: N"` with the number of items above 50 gold.
Expected output:
Potion: 30 gold
Elixir: 120 gold
Map: 45 gold
Rope: 80 gold
Cheap: Potion, Map
Expensive items: 2const items = [
{ name: "Potion", price: 30 },
{ name: "Elixir", price: 120 },
{ name: "Map", price: 45 },
{ name: "Rope", price: 80 }
];
// Write your code hereExercise 2 — Healing Station
The healing station treats the team one member at a time. Implement its three helpers with array methods.
Step 1: `needsHealing()` returns an array of the members whose HP is below their `maxHp`. Step 2: `healOne(name)` finds the member with that name and sets its HP to its `maxHp`, then prints `"name is fully healed"`. If the name isn't found, it prints `"No Codémon named name"`. Step 3: `firstInjuredIndex()` returns the position of the first member whose HP is below its max, or -1. Step 4: print `"Injured: N"`, heal `"Zap"`, try to heal `"Florachu"`, then print `"Injured: N"` again and `"First injured at: N"`.
Expected output:
Injured: 2
Zap is fully healed
No Codémon named Florachu
Injured: 1
First injured at: 2const team = [
{ name: "Aquachu", hp: 40, maxHp: 40 },
{ name: "Zap", hp: 5, maxHp: 30 },
{ name: "Metachu", hp: 20, maxHp: 60 }
];
// Write your functions here
// Use them hereExercise 3 — Tournament Stats
Each fighter has a list of results, `"W"` for a win and `"L"` for a loss. Compute the tournament stats with chains.
Step 1: `summaries` is an array of strings `"name: X wins"`, where X is the number of `"W"` in that fighter's results. Print each one. Step 2: `undefeated` is an array of the names of fighters with zero losses. Print `"Undefeated: N"` with how many there are. Step 3: print `"Total wins: N"` with the sum of all wins across fighters.
Expected output:
Aquachu: 2 wins
Pyrochu: 3 wins
Zap: 1 wins
Undefeated: 1
Total wins: 6const fighters = [
{ name: "Aquachu", results: ["W", "L", "W"] },
{ name: "Pyrochu", results: ["W", "W", "W"] },
{ name: "Zap", results: ["L", "W", "L"] }
];
// Write your code here