Drill

ProblemsJavaScript › payments

How much of this can still be refunded

easypaymentsJavaScript

An agent asks to refund an amount against an order that may already have been partly refunded.

refundableAmount(paid: int, refunded: int, requested: int) → int

Solve it in the editor →

Where you start

function refundableAmount(paid, refunded, requested) {
  
}

Worked examples

CallResult
refundableAmount(1000, 200, 500)500
refundableAmount(1000, 200, 900)800
refundableAmount(1000, 1000, 50)0
refundableAmount(1000, 0, -5)0

Hint

Work out what remains, then take the smaller of that and the request — floored at zero.

Reference solution in JavaScript
function refundableAmount(paid, refunded, requested) {
  if (requested <= 0) return 0;
  const left = paid - refunded;
  if (left <= 0) return 0;
  return Math.min(left, requested);
}

The same problem in another language

More payments problems in JavaScript