Drill

ProblemsTypeScript › inventory

Which parts need attention first

mediuminventoryTypeScript

The morning report lists every part sitting below its minimum, worst first, so the buyer knows where to start.

lowStock(parts: list<Sku>) → list<string>

Solve it in the editor →

Where you start

function lowStock(parts: Sku[]): string[] {
  
}

Worked examples

CallResult
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 TypeScript
function lowStock(parts: Sku[]): string[] {
  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);
}

The same problem in another language

More inventory problems in TypeScript