Drill

ProblemsJavaScript › billing

How much is still owed

easybillingJavaScript

Compare the amount due against a list of payments already received and return the shortfall.

balanceShortfall(due: int, payments: list<int>) → int

Solve it in the editor →

Where you start

function balanceShortfall(due, payments) {
  
}

Worked examples

CallResult
balanceShortfall(10000, [3000,4000])3000
balanceShortfall(5000, [5000])0
balanceShortfall(5000, [6000])0
balanceShortfall(1000, [])1000

Hint

Sum the payments and subtract from due.

Reference solution in JavaScript
function balanceShortfall(due, payments) {
  let paid = 0;
  for (const p of payments) paid += p;
  return Math.max(0, due - paid);
}

The same problem in another language

More billing problems in JavaScript