Problems › TypeScript › billing
How much is still owed
Compare the amount due against a list of payments already received and return the shortfall.
- Shortfall is due minus total payments, clamped at zero.
- An empty payment list means the full amount is still owed.
balanceShortfall(due: int, payments: list<int>) → int
Where you start
function balanceShortfall(due: number, payments: number[]): number {
}
Worked examples
| Call | Result |
|---|---|
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);
}