Most compound interest calculators hide the maths behind a form. The formula itself is short:
A = P * (1 + r/n)^(n*t) P is the principal, r the annual rate as a decimal, n the number of compounding periods per year and t the number of years. Monthly compounding means n = 12; daily means n = 365.
A 30-line version
function compound({ principal, annualRate, years, periodsPerYear = 12, monthlyDeposit = 0 }) { const r = annualRate / 100; const periods = years * periodsPerYear; const perPeriodRate = r / periodsPerYear; let balance = principal; const rows = []; for (let i = 1; i <= periods; i++) { balance = balance * (1 + perPeriodRate) + monthlyDeposit; if (i % periodsPerYear === 0) { rows.push({ year: i / periodsPerYear, balance: Math.round(balance) }); } } return rows; } Three details that change the answer more than people expect:
- Deposits timing. Adding the deposit after the interest accrues is different from adding it before. Pick one and stay consistent, otherwise your numbers drift from the bank's.
- Rounding. Round only for display. Rounding inside the loop compounds the error.
- Real return. Subtracting inflation is a separate calculation — do not bake it into the nominal formula, or you lose the ability to show both.
Try it
I built a version that shows the year-by-year table and the split between contributions and interest: https://mm-tools-6ln.pages.dev/en-compound-interest-calculator
The Vietnamese version with bank savings rates and a loan-payment calculator is here: https://mm-tools-6ln.pages.dev/tinh-lai-kep
Both run entirely in the browser — no data leaves the page.
