Javascript · Arrays (Basics)
Challenge
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Inventory Manager
Manage your bag during the adventure: 1. You pick up `"Rope"` then `"Torch"`: they go at the end of the bag 2. You find a `"Key"`: it must go at the very front of the bag 3. You use the last item: remove it and print `"Used: X"` 4. Print `"Items: N"` with the number of items left 5. If the bag contains the key, print `"You can open the door!"`, otherwise `"The door stays closed"`
Starter code
const bag = ["Potion", "Map"]; // Write your code here
Exercise 2 — Team Stats
`team` and `hp` go together, index by index. 1. For each Codémon, print `"X: Y HP"` 2. Count how many Codémon have HP strictly above 0 and print `"Ready to fight: N"` 3. Print `"Weakest: X"` with the name of the Codémon that has the lowest HP Your code must keep working if Codémon are added to both arrays.
Starter code
const team = ["Aquachu", "Pyrochu", "Florachu", "Zap"]; const hp = [40, 0, 25, 60]; // Write your code here
Exercise 3 — Capture Log
You walk through the tall grass and meet the Codémon listed in `encounters`, in order. `captured` starts empty. 1. For each encounter, if that Codémon is not yet in `captured`, add it and print `"New: X"` 2. If you already have it, print `"Already have X"` 3. After the loop, print `"Codédex: N"` with the number of different Codémon captured
Starter code
const encounters = ["Aquachu", "Zap", "Aquachu", "Metachu", "Zap", "Florachu"]; const captured = []; // Write your code here
Back to the course