Problems › TypeScript › billing
Prorate an annual subscription refund
When a subscriber cancels early, the unused portion of the annual fee is refunded proportionally.
- Refund equals annualAmount × (12 − monthsUsed) ÷ 12, using integer division (floor).
- monthsUsed is clamped to the range 0..12 before computing.
- monthsUsed 0 yields the full annualAmount; monthsUsed 12 yields 0.
prorateRefund(annualAmount: int, monthsUsed: int) → int
Where you start
function prorateRefund(annualAmount: number, monthsUsed: number): number {
}
Worked examples
| Call | Result |
|---|---|
prorateRefund(12000, 0) | 12000 |
prorateRefund(12000, 12) | 0 |
prorateRefund(12000, 3) | 9000 |
prorateRefund(10000, 5) | 5833 |
Hint
Clamp first, then multiply before dividing.
Reference solution in TypeScript
function prorateRefund(annualAmount: number, monthsUsed: number): number {
let m = Math.max(0, Math.min(12, monthsUsed));
return Math.floor(annualAmount * (12 - m) / 12);
}