Drill

ProblemsTypeScript › inventory

What the shelf is worth

easyinventoryTypeScript

Month-end valuation multiplies what is on each shelf by its unit cost and adds it up.

stockValue(rows: list<Row>) → int

Solve it in the editor →

Where you start

function stockValue(rows: Row[]): number {
  
}

Worked examples

CallResult
stockValue([{"qty":3,"unitCost":250},{"qty":2,"unitCost":100}])950
stockValue([{"qty":-4,"unitCost":500},{"qty":1,"unitCost":500}])500
stockValue([{"qty":0,"unitCost":900}])0
stockValue([])0

Hint

One pass, one accumulator, one guard.

Reference solution in TypeScript
function stockValue(rows: Row[]): number {
  let total = 0;
  for (const r of rows) if (r.qty > 0) total += r.qty * r.unitCost;
  return total;
}

The same problem in another language

More inventory problems in TypeScript