Javascript · Objects
Challenge
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Codémon Card
Build a complete Codémon object. 1. Properties: `name` `"Metachu"`, `type` `"metal"`, `level` 10, `hp` 90, `moves` `["Iron Punch"]` 2. `describe()` returns `"Metachu (metal) - level 10, 90 HP"` built from its own properties 3. `learn(move)` adds the move to its list and prints `"Metachu learned X!"` 4. `levelUp()` increases the level by 1 and the HP by 10 5. Call `learn` with `"Steel Wing"`, call `levelUp`, then print the description and `"Moves: N"`
Starter code
// Write your code here
Exercise 2 — Shop Inventory
The shop's stock is an object where each key is an item and each value is a quantity. 1. A customer buys 2 potions: lower the matching quantity 2. The shop receives a new item `elixir` with 5 units 3. `rope` is sold out: remove it from the stock entirely 4. Print every item as `"item: quantity"`, then `"Total units: N"` with the sum of all quantities
Starter code
const stock = { potion: 10, map: 3, rope: 0 }; // Write your code here
Exercise 3 — Arena Manager
The arena keeps a list of fighters as objects. Complete its two methods. 1. `addFighter(name, hp)` adds a new object with those two properties to `this.fighters` 2. `report()` prints `"name: X HP"` for each fighter, then `"Still standing: N"` with the number of fighters whose HP is above 0 3. Add `"Rockachu"` with 70 HP and `"Florachu"` with 0 HP, then call `report`
Starter code
const arena = { fighters: [ { name: "Zap", hp: 45 } ], // Write your methods here }; // Use the arena here
Back to the course