Drill

ProblemsTypeScript › billing

How much is still owed

easybillingTypeScript

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: number, payments: number[]): number {
  
}

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 TypeScript
function balanceShortfall(due: number, payments: number[]): number {
  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 TypeScript