A debt payoff simulator looks like ten lines of code. Each month: add interest, pay every minimum, throw the extra at one target debt. When a debt is cleared, its minimum joins the extra. Snowball sorts targets by smallest balance, avalanche by highest rate.
The subtle part is the month a debt clears. If a $50 minimum clears a $30 balance, only $20 is free this month. Next month the whole $50 is free. Count the whole $50 twice (once as "cleared" and once as "unused") and the snowball, which clears small debts early, looks faster than it is.
// Debt payoff, simulated month by month in integer cents. // Interest first, then minimums, then the extra (plus freed minimums) at one target. function simulate(debts, extraCents, order) { const d = debts.map(x => ({ ...x })); // { name, bal (cents), aprBp (basis points), min (cents) } let interest = 0, months = 0; const cleared = []; while (d.some(x => x.bal > 0)) { if (++months > 600) return null; // payments never outrun the interest let pool = extraCents; for (const x of d) { if (x.bal === 0) { pool += x.min; continue; } // cleared earlier: its whole minimum is free const it = Math.round(x.bal * x.aprBp / 120000); // monthly interest, rounded to the cent interest += it; x.bal += it; const pay = Math.min(x.min, x.bal); x.bal -= pay; pool += x.min - pay; // cleared this month: only the unused part is free if (x.bal === 0) cleared.push([x.name, months]); } for (const x of d.filter(x => x.bal > 0).sort(order)) { if (pool === 0) break; const hit = Math.min(pool, x.bal); x.bal -= hit; pool -= hit; if (x.bal === 0) cleared.push([x.name, months]); } } return { months, interest, cleared }; } const snowball = (a, b) => a.bal - b.bal; const avalanche = (a, b) => b.aprBp - a.aprBp || a.bal - b.bal; const debts = [ { name: 'card', bal: 480000, aprBp: 2390, min: 12000 }, { name: 'car', bal: 920000, aprBp: 690, min: 23000 }, { name: 'medical', bal: 90000, aprBp: 0, min: 5000 }, ]; const s = simulate(debts, 20000, snowball), a = simulate(debts, 20000, avalanche); console.log(s.months, s.interest, s.cleared, a.months, a.interest, a.cleared); Output for the sample (card $4,800 at 23.9%, car $9,200 at 6.9%, medical $900 at 0%, $200 extra):
29 210672 [['medical',4],['card',19],['car',29]] 29 193796 [['medical',18],['card',18],['car',29]] Both finish in month 29; avalanche pays $1,937.96 of interest, snowball $2,106.72. A floating-point version of the same loop (interest not rounded each month) gives $1,937.92 and $2,106.66. Which one is "right" depends on how your lender rounds; what matters is that the code picks one rule and tests it.
The property test
With the same monthly money, the avalanche should never pay more interest than the snowball. That is a good invariant to hammer with random cases:
for (let i = 0; i < 2000; i++) { const ds = Array.from({ length: 1 + rnd(5) }, (_, k) => ({ name: 'd' + k, bal: 1000 + rnd(2000000), aprBp: rnd(3000), min: 0 })); ds.forEach(x => x.min = Math.max(2500, Math.ceil(x.bal * 0.03))); const e = rnd(50000); const S = simulate(ds, e, snowball), A = simulate(ds, e, avalanche); if (S && A) assert.ok(A.interest <= S.interest + ds.length * 30); } The small tolerance is per-cent rounding: two schedules round interest on different balances, so they can differ by a few cents in the "wrong" direction. Without the tolerance the test fails on rounding noise, not on logic, and that is worth knowing before a user reports it.
If you want to compare against a running version, the same order logic is used in the free calculator at https://gettidycents.com/tools/debt-payoff-calculator.