Drill

ProblemsTypeScript › inventory

Will this fit in the bay

easyinventoryTypeScript

Goods-in scans a pallet and asks whether the bay it is headed for can take it.

canStore(item: Item?, onHand: int, capacity: int) → bool

Solve it in the editor →

Where you start

function canStore(item: Item | null, onHand: number, capacity: number): boolean {
  
}

Worked examples

CallResult
canStore({"name":"bolt","qty":3}, 0, 100)true
canStore({"name":"bolt","qty":5}, 10, 15)true
canStore({"name":"bolt","qty":5}, 10, 12)false
canStore({"name":"bolt","qty":0}, 0, 100)false

Hint

Two guards, then one comparison. Watch the boundary: equal to capacity still fits.

Reference solution in TypeScript
function canStore(item: Item | null, onHand: number, capacity: number): boolean {
  if (item === null || item.qty <= 0) return false;
  return onHand + item.qty <= capacity;
}

The same problem in another language

More inventory problems in TypeScript