Javascript - Challenge
Code Life
By Pierre Marsaa
Inventory Class
Model the player's bag as a class that manages its own items and enforces a capacity.
Step 1: Bag takes a capacity. Its constructor sets items to an empty array.
Step 2: add(item) pushes the item and prints "Added item" if there is room, otherwise prints "Bag full, dropped item". It returns the instance either way.
Step 3: count() returns the number of items, and has(item) returns whether the item is inside.
Step 4: create a bag of capacity 2, then in one chained line add Potion, Map and Rope. Print "Items: N" and "Has Rope: X".
Expected output:
Loading editor...
↗️
Loading editor...
↶
↷
💡 Show Solution
Continue