Skip to content
dreamcode
dreamcode
Projects
Inventory Diff
Independent projectJavaScript
Reward: +340 XP
PROJECT BRIEF

Inventory Diff

A shop counts its stock twice a day. Each count is an object mapping item names to quantities. Write diffInventory(before, after) that returns:

  • added: items that only exist in after
  • removed: items that only exist in before
  • changed: items in both whose quantity changed, as { item, from, to }

Sort added and removed alphabetically, and sort changed by item name.

Examples
diffInventory({ apples: 5, pears: 2, plums: 7 }, { apples: 3, plums: 7, kiwis: 4 })
{ added: ["kiwis"], removed: ["pears"], changed: [{ item: "apples", from: 5, to: 3 }] }
diffInventory({ tea: 1 }, { tea: 1 })
{ added: [], removed: [], changed: [] }
project.js
JAVASCRIPT
Saved as you type

Tests

0 of 3 passing
  • a busy morning
    diffInventory({ apples: 5, pears: 2, plums: 7 }, { apples: 3, plums: 7, kiwis: 4 })
    expected { added: ["kiwis"], removed: ["pears"], changed: [{ item: "apples", from: 5, to: 3 }] }
  • nothing changed
    diffInventory({ tea: 1 }, { tea: 1 })
    expected { added: [], removed: [], changed: [] }
  • sorted output
    diffInventory({ zinc: 1, iron: 2 }, { zinc: 4, iron: 1, gold: 1, copper: 3 })
    expected { added: ["copper", "gold"], removed: [], changed: [{ item: "iron", from: 2, to: 1 }, { item: "zinc", from: 1, to: 4 }] }
On the line
+340 XP
Pass all 3 tests to claim it.