Problems › JavaScript › billing
Refund for partially used service
A customer paid upfront but only used part of the service. Refund the unused portion.
- Refund equals paid minus (usedUnits × unitPrice), clamped at zero.
- If usage cost meets or exceeds what was paid, no refund.
partialRefund(paid: int, usedUnits: int, unitPrice: int) → int
Where you start
function partialRefund(paid, usedUnits, unitPrice) {
}
Worked examples
| Call | Result |
|---|---|
partialRefund(1000, 40, 10) | 600 |
partialRefund(1000, 200, 10) | 0 |
partialRefund(500, 0, 50) | 500 |
partialRefund(1000, 100, 10) | 0 |
Hint
Multiply usage by price, subtract from what was paid, and clamp.
Reference solution in JavaScript
function partialRefund(paid, usedUnits, unitPrice) {
const cost = usedUnits * unitPrice;
return paid > cost ? paid - cost : 0;
}