Problem overview
The method restock receives inventory, an object that maps item names to counts, plus an item and an amount. A delivery brings amount more of item. The method adds that amount to the item's count and returns the updated object. Every other entry stays the same.
With inventory = {"apples": 5, "bananas": 2, "cherries": 10}, item = "bananas" and amount = 3, the count of bananas goes from 2 to 5. The result is {"apples": 5, "bananas": 5, "cherries": 10}. A count of 0 works the same way: {"milk": 0} with 12 more milk becomes {"milk": 12}.
Approach: read, add, store
An entry of an object can be used on the left side of an assignment, just like a variable. inventory[item] += amount is shorthand for inventory[item] = inventory[item] + amount. It reads the old count, adds amount, and stores the sum back under the same key. No other key is touched.
The item is always already in the inventory, so the old count exists and is a number. The lesson doesn't need to create new entries.
The other compound assignments work the same way on an entry: -= would record a sale, and ++ would add exactly one. Because the whole expression is evaluated against the same key, the lesson never needs a temporary variable to hold the old count.
Step by step
- Add with
+=. Use brackets,inventory[item], becauseitemis a variable holding the key. - Return
inventory. The object was changed in place, so returning it hands back all the entries with one count updated.
The solution in JavaScript
class Solution {
restock(inventory, item, amount) {
inventory[item] += amount;
return inventory;
}
}
Details worth knowing:
- The object changes in place. JavaScript passes objects by reference, so the function and its caller share the same object. After
const stock = {"pens": 7, "paper": 3}andnew Solution().restock(stock, "pens", 1),stockitself is{"pens": 8, "paper": 3}. The returned value andstockare one object. - Bracket notation.
inventory.itemwould look for a key literally nameditem. Brackets use the variable's value. +=on a missing key. If the key were missing, the old value would beundefined, andundefined + 3isNaN. That can't happen here, because the lesson guarantees thatitemis a key.- Entry order. Updating a value doesn't move the entry, and the lesson doesn't care about order anyway.
If the caller needs to keep the original object, copy it first. The expression {...inventory, [item]: inventory[item] + amount} builds a new object with the same entries and one updated count, and leaves inventory untouched. The lesson uses the simpler in-place update.
Edge cases and common mistakes
- A count of zero.
0 + amountisamount, so sold-out items restock correctly. - Using
=instead of+=. That would replace the count withamountand lose the old stock. For"bananas"the result would be 3, not 5. - Forgetting to return. The object changes in place, but the method has to return it to hand it back to the lesson's tests.
- Changing the wrong key. Writing
inventory[amount]orinventory["item"]adds a stray entry instead of updating the item's count.
Testing
{"apples": 5, "bananas": 2, "cherries": 10},"bananas", 3 gives{"apples": 5, "bananas": 5, "cherries": 10}.{"milk": 0},"milk", 12 gives{"milk": 12}.{"pens": 7, "paper": 3},"pens", 1 gives{"pens": 8, "paper": 3}.
