Start learning free

LESSON 133 OF 671 · DICTIONARIES - CHALLENGES

Sell an Item in JavaScript: Update or Delete a Key

Practice Read the full explanation

You get inventory, a dictionary from item names to how many of each item a shop has. You also get the item a customer just bought.

Sell one item and return the updated dictionary.

The count of item drops by 1. When it reaches 0, remove item from the dictionary. The order of the entries doesn't matter.

 

Example 1:

Input: inventory = {"apples": 5, "pears": 2}, item = "apples"
Output: {"apples": 4, "pears": 2}
Explanation: One apple is sold, so 5 - 1 = 4 apples are left.

Example 2:

Input: inventory = {"cups": 1, "plates": 3}, item = "cups"
Output: {"plates": 3}
Explanation: The last cup is sold. No cups are left, so "cups" is removed.

Example 3:

Input: inventory = {"pen": 1}, item = "pen"
Output: {}
Explanation: The only pen is sold. Nothing is left in the shop.

 

Constraints:

  • item is always a key of inventory.
  • Every count is at least 1.
  • Item names are single lowercase words.
STEP 1 OF 4
    Ctrl + Enter to check

    JAVASCRIPT · DICTIONARIES - CHALLENGES

    Sell An Item in JavaScript, explained

    Problem overview

    The method sellItem receives inventory, an object that maps item names to how many of each item a shop has, and the item a customer just bought. It sells one item and returns the updated object. The count of item drops by 1, and when it reaches 0 the item is removed from the object. The order of the entries does not matter.

    The constraints are friendly: item is always a key of inventory, every count is at least 1, and item names are single lowercase words. So you never have to handle a missing item or a count of 0 on the way in.

    The examples:

    • Selling "apples" from {"apples": 5, "pears": 2} gives {"apples": 4, "pears": 2}.
    • Selling "cups" from {"cups": 1, "plates": 3} gives {"plates": 3}. The last cup is sold, so the key goes.
    • Selling "pen" from {"pen": 1} gives {}.

    Approach

    Every sale lowers a count by 1. What happens next depends on what is left. If some are left, store the new count. If none are, the entry should not stay with a 0: delete it. That is one decision with two branches, so an if and an else fit.

    Work out the count after the sale first, in a variable. Then both branches can use it, and the test reads clearly.

    Step by step

    1. Compute how many are left after the sale: the count of item minus 1, in a variable named remaining, declared with let. Reading inventory[item] is safe because the item is always a key with a count of at least 1.
    2. Write an if that checks whether remaining is 0. Its body deletes item from inventory with the delete operator, which removes the key and its value.
    3. Add an else branch whose body stores remaining under the key item. Some are still left, so the item stays with its new count.
    4. After the else, return inventory. Only the entry of item changed, or it is gone.

    The solution in JavaScript

    class Solution {
        sellItem(inventory, item) {
            let remaining = inventory[item] - 1;
            if (remaining == 0) {
                delete inventory[item];
            } else {
                inventory[item] = remaining;
            }
            return inventory;
        }
    }
    

    Details worth knowing

    The test uses ==, which compares loosely. With two numbers it behaves like ===, and in everyday JavaScript === is the safer habit. Both work here, because remaining is always a number.

    The delete operator removes the whole entry. Setting the value to undefined would leave the key behind, and setting it to 0 would leave a key with a count of 0, so the shop would still list an item it no longer has.

    The method changes the object it was given and returns that same object.

    Testing

    Running the method on the three examples and printing each result with JSON.stringify gives:

    {"apples":4,"pears":2}
    {"plates":3}
    {}
    

    Edge cases and common mistakes

    • Leaving a zero behind. Always writing inventory[item] = remaining turns the last cup into {"cups":0}. The object still has one key, and it should be empty.
    • Deleting too early. If you delete first and read the count afterwards, the count is gone. Compute remaining before the if.
    • Setting undefined. Assigning undefined to a key leaves it in the object: "cups" in inv stays true.
    • Using the old count. Testing inventory[item] against 0 never fires, because counts start at 1 or more. Test the number left after the sale.
    Keep going with JavaScript500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free