Both payoff methods are a tiny simulation: accrue interest, pay every minimum, then send whatever is left to one target debt. The only difference is how you pick the target.
- Snowball: smallest balance first.
- Avalanche: highest APR first.
Here is the whole thing.
function payoff(debts, extra, method) { const ds = debts.map(d => ({ ...d })); const order = method === 'snowball' ? (a, b) => a.balance - b.balance : (a, b) => b.apr - a.apr; const pool = ds.reduce((s, d) => s + d.min, 0) + extra; let month = 0, interest = 0; while (ds.some(d => d.balance > 0.005) && month < 600) { month++; for (const d of ds) { // 1. interest accrues const i = d.balance * d.apr / 100 / 12; d.balance += i; interest += i; } let left = pool; // 2. minimums on every open debt for (const d of ds) { const pay = Math.min(d.min, d.balance); d.balance -= pay; left -= pay; } for (const d of ds.filter(d => d.balance > 0.005).sort(order)) { const pay = Math.min(left, d.balance); // 3. the rest goes to the target d.balance -= pay; left -= pay; if (left <= 0) break; } } return { months: month, interest: Math.round(interest) }; } const debts = [ { name: 'Personal loan', balance: 1500, apr: 9, min: 60 }, { name: 'Credit card', balance: 6000, apr: 24, min: 120 }, { name: 'Car loan', balance: 9000, apr: 6.5, min: 250 }, ]; console.log(payoff(debts, 200, 'snowball')); // { months: 31, interest: 2978 } console.log(payoff(debts, 200, 'avalanche')); // { months: 31, interest: 2698 } Three details that are easy to get wrong:
- Keep the pool constant. When a debt is paid off, its minimum stays in
pooland rolls to the next target. That rollover is the "snowball". If you recompute the pool from open debts, you've built a different (slower) plan. - Interest before payment. Monthly interest is
balance × APR / 12, applied before this month's payments. Card issuers use daily balances, so treat the result as an estimate. - Guard against forever. If a minimum doesn't cover the interest, the balance never falls. The
month < 600cap stops the loop; in a real tool, detect it and tell the user.
In this example both methods finish in the same month, and avalanche saves about $280 in interest. Snowball clears the personal loan first, which is the early win some people need to keep going. The code can't tell you which matters more to you.
A browser version with a chart and payoff order is free here (no sign-up, nothing stored): https://eztools.space.fast/tools/debt-payoff-calculator/
I make the EZ Tools calculators and sell spreadsheet versions on Gumroad; this post contains no affiliate links. Drafted with AI assistance and checked by me; the code was run as shown. Not financial advice.
