Start learning free

LESSON 130 OF 671 · DICTIONARIES - CHALLENGES

Restock in JavaScript: Updating an Object Value with +=

Practice Read the full explanation

A shop keeps its stock in inventory, a dictionary from item names to how many of each item it has. A delivery brings amount more of item. Add amount to the count of item and return the updated dictionary.

Only the count of item changes: every other entry stays as it is. The order of the entries doesn't matter.

 

Example 1:

Input: inventory = {"apples": 5, "bananas": 2, "cherries": 10}, item = "bananas", amount = 3
Output: {"apples": 5, "bananas": 5, "cherries": 10}
Explanation: "bananas" had a count of 2, and 2 + 3 = 5. The apples and the cherries keep their counts.

Example 2:

Input: inventory = {"milk": 0}, item = "milk", amount = 12
Output: {"milk": 12}
Explanation: A count of 0 means the milk was sold out. The delivery brings it to 0 + 12 = 12.

Example 3:

Input: inventory = {"pens": 7, "paper": 3}, item = "pens", amount = 1
Output: {"pens": 8, "paper": 3}
Explanation: One more pen: 7 + 1 = 8.

 

Constraints:

  • item is always a key of inventory.
  • amount is at least 1.
  • Every count is 0 or more.
  • Item names are single lowercase words, and they are all different.
STEP 1 OF 2
    Ctrl + Enter to check

    JAVASCRIPT · DICTIONARIES - CHALLENGES

    Restock in JavaScript, explained

    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

    1. Add with +=. Use brackets, inventory[item], because item is a variable holding the key.
    2. 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} and new Solution().restock(stock, "pens", 1), stock itself is {"pens": 8, "paper": 3}. The returned value and stock are one object.
    • Bracket notation. inventory.item would look for a key literally named item. Brackets use the variable's value.
    • += on a missing key. If the key were missing, the old value would be undefined, and undefined + 3 is NaN. That can't happen here, because the lesson guarantees that item is 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 + amount is amount, so sold-out items restock correctly.
    • Using = instead of +=. That would replace the count with amount and 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] or inventory["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}.
    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free