Compound interest calculator in JavaScript
Intermediate · 4 min read
Savings accounts, fixed deposits, loans and credit cards all grow by compound interest: each period, you earn interest on your interest as well as on the money you put in. A compound interest calculator is a small piece of code, but it raises two real questions. How often is interest added? And when do you round to a whole kobo?
In this tutorial you will take a deposit of 250,000 naira at 12% a year for five years. You will work out simple interest, then compound it yearly, monthly, daily and continuously. Then you will hold the balance in whole kobo, round it every month, and compare that with rounding once at the end. You need to know ** (powers), for loops and arrow functions.
Simple versus compound
Simple interest is paid only on the starting amount. 12% of 250,000 is 30,000 a year, so five years adds 150,000.
Compound interest adds each year's interest to the balance, so the next year's 12% is worked out on a bigger number. After year one you have 280,000, and year two pays 12% of that, not of 250,000.
The formula
With P the starting amount, r the yearly rate as a decimal (0.12), n the number of times interest is added per year and t the number of years:
total = P * (1 + r / n) ** (n * t)
Each period multiplies the balance by 1 + r / n, and there are n * t periods.
Here is the exercise. If you want to try it on your own first, go ahead. Otherwise, read on and we will build it together.
95 Compound interest, and where the kobo goes
fairTake a deposit of 250,000 naira at 12% a year for five years. Give the simple-interest total, then compound annually, monthly, daily and continuously. Then hold the balance in integer kobo, compound it month by month rounding each time, and compare with the closed-form answer.
Stuck? Three hints, smallest first
- 1. The closed form is
P * (1 + r / n) ** (n * t), withncompounding periods a year. - 2. The continuous limit is
P * Math.exp(r * t), and nothing discrete can beat it. - 3. Rounding to whole kobo every month is not the same as rounding once at the end.
Step 1: simple interest
const simple = (principal, rate, years) => principal * (1 + rate * years);
const naira = 250000;
const rate = 0.12;
console.log(simple(naira, rate, 5)); // 400000
250,000 plus five lots of 30,000 is 400,000. Exactly, with no rounding at all.
Step 2: compound once a year
const compound = (principal, rate, perYear, years) =>
principal * (1 + rate / perYear) ** (perYear * years);
console.log(compound(naira, rate, 1, 5)); // 440585.4208000002
console.log(compound(naira, rate, 1, 5).toFixed(2)); // 440585.42
Compounding beats simple interest by about 40,585 naira. The raw number has a tiny decimal error at the end, which is normal for JavaScript numbers. toFixed(2) rounds it to two places for display.
Step 3: more often
console.log(compound(naira, rate, 12, 5)); // 454174.1746410228
console.log(compound(naira, rate, 365, 5)); // 455484.7832005464
Monthly pays more than yearly, and daily pays a little more again. But the gains shrink fast: going from 12 times a year to 365 adds only about 1,300 naira.
Step 4: the ceiling, continuous compounding
If interest were added every instant, the total would be P * e ** (r * t), where e is a special constant (about 2.718). Math.exp(x) works out e ** x.
const continuous = (principal, rate, years) => principal * Math.exp(rate * years);
console.log(continuous(naira, rate, 5)); // 455529.70009762724
No matter how often you compound, you can never beat this number. Daily compounding is already within 45 naira of it.
Step 5: what 12% really pays
A bank that says "12% a year, compounded monthly" is actually paying a bit more than 12% over a full year.
const effectiveAnnual = (rate, perYear) => (1 + rate / perYear) ** perYear - 1;
console.log(effectiveAnnual(rate, 12)); // 0.12682503013196977
That is about 12.68%. This effective annual rate is the fair number to compare between two accounts that compound differently.
Step 6: money in whole kobo
Real money can't have fractions of a kobo. So hold the balance as a whole number of kobo (100 kobo to a naira) and round on purpose.
const kobo = naira * 100;
const closedFormKobo = (startKobo, rate, months) =>
Math.round(startKobo * (1 + rate / 12) ** months);
const monthByMonthKobo = (startKobo, rate, months) => {
let balance = startKobo;
for (let m = 0; m < months; m++) balance = Math.round(balance * (1 + rate / 12));
return balance;
};
console.log(closedFormKobo(kobo, rate, 60)); // 45417417
console.log(monthByMonthKobo(kobo, rate, 60)); // 45417418
closedFormKobo uses the formula and rounds once at the end. monthByMonthKobo adds each month's interest and rounds to a whole kobo every time, the way a bank statement does.
They differ by one kobo. Neither is a bug. They are two different rules, and which one your app follows is a decision that should be written down, not left to whichever line of code was written first. Real accounts usually round every period, because each month's interest is actually paid in.
Step 7: how long to double
console.log(Math.log(2) / Math.log(1 + rate)); // 6.1162553741996994
console.log(72 / 12); // 6
At 12% a year, money doubles in about 6.12 years. The rule of 72 is a quick mental shortcut: divide 72 by the rate in percent. It gives 6, close enough for a rough guess.
The full solution
The answer panel has compound, simple, continuous, effectiveAnnual and both kobo versions, and prints every total above, the one-kobo difference and the doubling time. Press Run to see the output.
const compound = (principal, rate, perYear, years) =>
principal * (1 + rate / perYear) ** (perYear * years);
const simple = (principal, rate, years) => principal * (1 + rate * years);
const continuous = (principal, rate, years) => principal * Math.exp(rate * years);
// Effective annual rate: what 12% nominal, compounded monthly, really pays.
const effectiveAnnual = (rate, perYear) => (1 + rate / perYear) ** perYear - 1;
// The same five years in integer kobo, rounded to a whole kobo every month.
const monthByMonthKobo = (startKobo, rate, months) => {
let balance = startKobo;
for (let m = 0; m < months; m++) balance = Math.round(balance * (1 + rate / 12));
return balance;
};
const closedFormKobo = (startKobo, rate, months) =>
Math.round(startKobo * (1 + rate / 12) ** months);
const naira = 250000;
const kobo = naira * 100;
const rate = 0.12;
console.log('simple, 5 years :', simple(naira, rate, 5));
console.log('annual :', compound(naira, rate, 1, 5), '->', compound(naira, rate, 1, 5).toFixed(2));
console.log('monthly :', compound(naira, rate, 12, 5), '->', compound(naira, rate, 12, 5).toFixed(2));
console.log('daily :', compound(naira, rate, 365, 5));
console.log('continuous :', continuous(naira, rate, 5));
console.log('effective annual :', effectiveAnnual(rate, 12));
console.log('kobo, closed form :', closedFormKobo(kobo, rate, 60));
console.log('kobo, month by month:', monthByMonthKobo(kobo, rate, 60));
console.log('difference :', monthByMonthKobo(kobo, rate, 60) - closedFormKobo(kobo, rate, 60), 'kobo');
console.log('doubling time :', Math.log(2) / Math.log(1 + rate), 'rule of 72 says', 72 / 12);
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('simple interest gives exactly 400000 naira, with no rounding anywhere', simple(naira, rate, 5), 400000);
test('compounding once a year gives 440585.4208000002, which is 440585.42', [compound(naira, rate, 1, 5), compound(naira, rate, 1, 5).toFixed(2)], [440585.4208000002, '440585.42']);
test('monthly beats annual, daily beats monthly, and none of them beat continuous', [compound(naira, rate, 12, 5), compound(naira, rate, 365, 5) > compound(naira, rate, 12, 5), continuous(naira, rate, 5) > compound(naira, rate, 365, 5)], [454174.1746410228, true, true]);
test('12% nominal compounded monthly is really 12.6825% a year', Number(effectiveAnnual(rate, 12).toFixed(6)), 0.126825);
test('in kobo, the closed form comes to 45417417 kobo', closedFormKobo(kobo, rate, 60), 45417417);
test('rounding every month instead gives 45417418, one kobo more', [monthByMonthKobo(kobo, rate, 60), monthByMonthKobo(kobo, rate, 60) - closedFormKobo(kobo, rate, 60)], [45417418, 1]);
test('both are whole kobo, which is the only property a ledger cares about', [Number.isInteger(closedFormKobo(kobo, rate, 60)), Number.isInteger(monthByMonthKobo(kobo, rate, 60))], [true, true]);
test('at 12% the balance doubles in 6.12 years, near enough the rule of 72', [Number((Math.log(2) / Math.log(1 + rate)).toFixed(6)), 72 / 12], [6.116255, 6]);
test('and compounding annually beats simple interest by over 40000 naira', Math.round(compound(naira, rate, 1, 5) - simple(naira, rate, 5)), 40585);
The shape of the formula is the lesson: P * (1 + r / n) ** (n * t). Raise the number of compounding periods and the total rises, but with sharply diminishing returns — annual 440,585.42, monthly 454,174.17, daily 455,484.78, and continuous 455,529.70, which is the ceiling that P * e ** (r * t) defines. A bank quoting "12% a year, compounded monthly" is paying an effective 12.6825%, and the effective annual rate is the only number worth comparing between products.
The kobo part is the part that causes arguments. Rounding to a whole kobo every month gives 45,417,418 kobo; applying the closed form once and rounding at the end gives 45,417,417. One kobo. Neither is a bug — they are two different products, and which one you implement is a policy decision that belongs in writing, not in whichever line of code happened to get written first. Real ledgers round every period, because each period's interest is actually credited.
Note that the money is held as integer kobo, not as a decimal number of naira. That is the rule from exercise 15 and it does not stop applying just because the arithmetic got more interesting: keep the smallest unit as an integer, round explicitly at defined moments, and never let a fractional kobo exist between two lines of code.
Common mistakes
- Using 12 instead of 0.12. The rate goes into the formula as a decimal. 12% is 0.12.
- Rounding only for display. If money is credited each month, round each month, in whole kobo, and keep it as a whole number.
- Comparing quoted rates with different compounding. Compare the effective annual rate instead.
Try it yourself
- How much would 250,000 naira grow to in 10 years at 12%, compounded monthly?
- Write
monthsToReach(startKobo, rate, targetKobo)that counts how many months ofmonthByMonthKobo-style rounding it takes to reach a target. - Compare 12% compounded monthly with 12.5% compounded once a year. Which pays more?