Problems › TypeScript › inventory
What the shelf is worth
Month-end valuation multiplies what is on each shelf by its unit cost and adds it up.
- Quantity and cost are both in whole units; the answer is in minor currency units.
- Negative quantities are counting errors — skip those rows entirely.
- An empty shelf is worth nothing.
stockValue(rows: list<Row>) → int
Where you start
function stockValue(rows: Row[]): number {
}
Worked examples
| Call | Result |
|---|---|
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;
}