Problems › JavaScript › inventory
Which parts need attention first
The morning report lists every part sitting below its minimum, worst first, so the buyer knows where to start.
- A part is short when what is on hand is strictly below its minimum.
- Order by shortfall, largest first.
- Parts equally short are listed by code, A to Z.
lowStock(parts: list<Sku>) → list<string>
Where you start
function lowStock(parts) {
}
Worked examples
| Call | Result |
|---|---|
lowStock([{"code":"B-2","onHand":4,"minLevel":10},{"code":"A-1","onHand":20,"minLevel":10},{"code":"C-3","onHand":0,"minLevel":30}]) | ["C-3","B-2"] |
lowStock([{"code":"Z-9","onHand":5,"minLevel":10},{"code":"A-4","onHand":1,"minLevel":6}]) | ["A-4","Z-9"] |
lowStock([{"code":"A-1","onHand":10,"minLevel":10}]) | [] |
lowStock([]) | [] |
Hint
Filter, then sort with a comparator that falls back to the code when the shortfalls match.
Reference solution in JavaScript
function lowStock(parts) {
return parts
.filter((p) => p.onHand < p.minLevel)
.sort((a, b) => (b.minLevel - b.onHand) - (a.minLevel - a.onHand) || a.code.localeCompare(b.code))
.map((p) => p.code);
}