Drill

ProblemsJavaScript › billing

Prorate an annual subscription refund

mediumbillingJavaScript

When a subscriber cancels early, the unused portion of the annual fee is refunded proportionally.

prorateRefund(annualAmount: int, monthsUsed: int) → int

Solve it in the editor →

Where you start

function prorateRefund(annualAmount, monthsUsed) {
  
}

Worked examples

CallResult
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 JavaScript
function prorateRefund(annualAmount, monthsUsed) {
  let m = Math.max(0, Math.min(12, monthsUsed));
  return Math.floor(annualAmount * (12 - m) / 12);
}

The same problem in another language

More billing problems in JavaScript