Problems › TypeScript › finance
Price per unit
A bulk bin lists a total for a quantity of identical items. What does a single unit cost?
- Price per unit is total ÷ quantity, rounded down to whole minor units.
- A quantity of zero or less has no per-unit price: return 0.
pricePerUnit(totalMinor: int, quantity: int) → int
Where you start
function pricePerUnit(totalMinor: number, quantity: number): number {
}
Worked examples
| Call | Result |
|---|---|
pricePerUnit(1000, 4) | 250 |
pricePerUnit(1000, 3) | 333 |
pricePerUnit(100, 10) | 10 |
pricePerUnit(0, 5) | 0 |
Hint
Divide and drop the remainder.
Reference solution in TypeScript
function pricePerUnit(totalMinor: number, quantity: number): number {
if (quantity <= 0) return 0;
return Math.floor(totalMinor / quantity);
}