Calculate a loan repayment schedule in JavaScript
Advanced · 5 min read
When someone borrows money and pays it back in monthly instalments, they get a repayment schedule: a table with a due date, an amount to pay and the balance left after each payment. Loan apps, school fee plans and "buy now, pay later" checkouts all show one. The last line must read exactly ₦0.00. A schedule that ends with five kobo still owing will trigger reminders and late fees.
In this tutorial you will build the schedule for a ₦120,000 loan over seven months at 2% interest a month, and print it as a neat statement. It brings together several small skills: money in kobo, splitting an amount fairly, adding months to a date, and lining up columns. You need to know loops, objects and padStart. Each piece is explained as it comes up.
The plan
Each month, the borrower pays two things:
- Principal: a share of the original ₦120,000.
- Interest: 2% of whatever is still owed, called the balance.
As the balance shrinks, so does the interest. So the first instalment is the biggest and the last is the smallest.
Why we count in kobo
Decimals like 0.1 can't be stored exactly in binary, and tiny errors add up across rows. Whole numbers are always exact. So every amount here is a whole number of kobo (100 kobo is ₦1), and we only turn it into naira when printing.
console.log(0.1 + 0.2); // 0.30000000000000004
console.log(10 + 20); // 30
Here is the exercise. Try it on your own first if you like, or read on and we will build it together.
150 A repayment schedule that clears to zero
hardLast one, and it uses most of the section. Spread a ₦120,000 loan over seven monthly instalments at 2% interest a month on the balance outstanding. Work in kobo, put the due date on the same day of each month with February clamped, let the last instalment absorb the rounding, and print a statement whose columns line up and whose balance ends at exactly zero.
Stuck? Three hints, smallest first
- 1. Divide the principal with
Math.floor, then give the remainder to the final instalment. - 2. Interest is charged on the balance still outstanding, so it falls every month.
- 3. Keep every figure an integer number of kobo and format only when printing.
Step 1: the inputs
const LOAN = 12000000; // ₦120,000 in kobo
const MONTHS = 7;
const RATE_BP = 200; // 2% a month, in basis points
const START = new Date(2025, 11, 31);
console.log(LOAN / 100); // 120000
console.log(RATE_BP / 10000); // 0.02
A basis point is one hundredth of a percent, so 200 basis points is 2%. Storing the rate as the whole number 200 keeps even the rate free of decimals. The loan starts on 31 December 2025 (months count from 0, so 11 is December).
Step 2: split the principal fairly
₦120,000 doesn't divide evenly into seven:
const LOAN = 12000000;
const flat = Math.floor(LOAN / 7);
console.log(LOAN / 7); // 1714285.7142857143
console.log(flat); // 1714285
console.log(flat * 7); // 11999995
console.log(LOAN - flat * 6); // 1714290
Math.floor rounds down to a whole kobo. But seven of those add up to 5 kobo short of the loan. Those 5 kobo have to go somewhere, or the balance never reaches zero. We give them to the last instalment: it pays whatever balance is left, which is 1714290 kobo. Every earlier row stays the same, which is easy to explain to a customer.
Step 3: interest on what is still owed
const interestOn = (balance) => Math.round((balance * 200) / 10000);
console.log(interestOn(12000000)); // 240000: ₦2,400 in the first month
console.log(interestOn(10285715)); // 205714: less in the second month
Multiply first, then divide, then round to a whole kobo. Because the balance falls each month, the interest falls with it.
Step 4: due dates that don't skip a month
The loan starts on the 31st, so we want a due date on the 31st of every month. February has no 31st. Ask JavaScript for one anyway and it rolls over:
const d = new Date(2026, 1, 31); // "31 February 2026"
console.log(d.getMonth(), d.getDate()); // 2 3: it became 3 March
That would skip February completely. The fix is to clamp the day: find the last day of the target month and never go past it.
function addMonths(date, n) {
const month = date.getMonth() + n;
const last = new Date(date.getFullYear(), month + 1, 0).getDate();
return new Date(date.getFullYear(), month, Math.min(date.getDate(), last));
}
const feb = addMonths(new Date(2025, 11, 31), 2);
console.log(feb.getFullYear(), feb.getMonth(), feb.getDate()); // 2026 1 28
Day 0 of a month means "the day before the 1st", which is the last day of the previous month. So new Date(year, month + 1, 0).getDate() gives the length of month. A month number past 11 simply moves into the next year, so month 13 of 2025 is February 2026.
Step 5: build the rows
Now put it together. Each pass of the loop works out the interest on the current balance, takes off the principal, and saves a row:
const stamp = (d) =>
d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0');
function addMonths(date, n) {
const month = date.getMonth() + n;
const last = new Date(date.getFullYear(), month + 1, 0).getDate();
return new Date(date.getFullYear(), month, Math.min(date.getDate(), last));
}
function schedule(loan, months, rateBp, start) {
const flat = Math.floor(loan / months);
let balance = loan;
const rows = [];
for (let i = 1; i <= months; i++) {
const interest = Math.round((balance * rateBp) / 10000);
const principal = i === months ? balance : flat;
balance -= principal;
rows.push({ i, due: stamp(addMonths(start, i)), principal, interest, pay: principal + interest, balance });
}
return rows;
}
const rows = schedule(12000000, 7, 200, new Date(2025, 11, 31));
console.log(rows[0]);
// { i: 1, due: '2026-01-31', principal: 1714285, interest: 240000, pay: 1954285, balance: 10285715 }
console.log(rows[6].principal, rows[6].balance); // 1714290 0
console.log(rows.map((r) => r.due).join(' '));
// 2026-01-31 2026-02-28 2026-03-31 2026-04-30 2026-05-31 2026-06-30 2026-07-31
Interest is worked out before the balance drops, because it's charged on what was owed during the month. i === months ? balance : flat is the remainder rule from Step 2: the last row pays everything that's left. stamp writes a date as YYYY-MM-DD, padding the month and day to two digits.
Step 6: print a statement that lines up
naira turns kobo into text like ₦19,542.85. Math.trunc(kobo / 100) is the whole naira, kobo % 100 is the kobo, and group adds the thousands commas. Then padStart pushes every amount to the same width, so the columns line up on the right:
const group = (n) => String(n).replace(/\B(?=(\d{3})+$)/g, ',');
const naira = (kobo) => '\u20a6' + group(Math.trunc(kobo / 100)) + '.' + String(kobo % 100).padStart(2, '0');
console.log(naira(1954285)); // ₦19,542.85
console.log(naira(0)); // ₦0.00
const line = String(1).padStart(2) + ' ' + '2026-01-31' + ' ' + naira(1954285).padStart(12) + ' ' + naira(10285715).padStart(13);
console.log(line); // 1 2026-01-31 ₦19,542.85 ₦102,857.15
console.log(line.length); // 43
'\u20a6' is the naira sign written as a code, so the file only needs plain letters. The regular expression in group finds each spot that has a multiple of three digits after it and puts a comma there.
Every line of the statement comes out 43 characters wide. The full statement looks like this:
# due date instalment balance
1 2026-01-31 ₦19,542.85 ₦102,857.15
2 2026-02-28 ₦19,199.99 ₦85,714.30
3 2026-03-31 ₦18,857.14 ₦68,571.45
4 2026-04-30 ₦18,514.28 ₦51,428.60
5 2026-05-31 ₦18,171.42 ₦34,285.75
6 2026-06-30 ₦17,828.57 ₦17,142.90
7 2026-07-31 ₦17,485.76 ₦0.00
total repaid ₦129,600.01
The balance ends at exactly ₦0.00, and the principal parts add back to ₦120,000 to the kobo. The instalments fall each month because the interest falls with the balance.
Banks often use a different formula that makes every payment the same size, but it builds on the same habits: whole kobo, and a remainder that has somewhere to go.
The full solution
The answer panel has the inputs, naira, stamp, addMonths and schedule, and prints the statement with the total repaid. The checks cover the zero balance, the principal total, the five-kobo remainder, falling interest, the clamped due dates, whole-kobo figures, each balance against the row above, and the line widths. Press Run to see the output.
const LOAN = 12000000; // ₦120,000 in kobo
const MONTHS = 7;
const RATE_BP = 200; // 2% a month, in basis points
const START = new Date(2025, 11, 31);
const group = (n) => String(n).replace(/\B(?=(\d{3})+$)/g, ',');
const naira = (kobo) => '\u20a6' + group(Math.trunc(kobo / 100)) + '.' + String(kobo % 100).padStart(2, '0');
const stamp = (d) =>
d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0');
// From exercise 127: clamp the day into the month it lands in.
function addMonths(date, n) {
const month = date.getMonth() + n;
const last = new Date(date.getFullYear(), month + 1, 0).getDate();
return new Date(date.getFullYear(), month, Math.min(date.getDate(), last));
}
function schedule(loan, months, rateBp, start) {
const flat = Math.floor(loan / months); // the even share, rounded down
let balance = loan;
const rows = [];
for (let i = 1; i <= months; i++) {
const interest = Math.round((balance * rateBp) / 10000);
const principal = i === months ? balance : flat; // the last one takes what is left
balance -= principal;
rows.push({ i, due: stamp(addMonths(start, i)), principal, interest, pay: principal + interest, balance });
}
return rows;
}
const rows = schedule(LOAN, MONTHS, RATE_BP, START);
const lines = rows.map(
(r) => String(r.i).padStart(2) + ' ' + r.due + ' ' + naira(r.pay).padStart(12) + ' ' + naira(r.balance).padStart(13)
);
console.log(' # due date instalment balance');
for (const line of lines) console.log(line);
console.log('total repaid', naira(rows.reduce((s, r) => s + r.pay, 0)));
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('seven instalments, and the balance ends at exactly zero', [rows.length, rows[6].balance], [7, 0]);
test('the principal repaid adds back to the loan, to the kobo', rows.reduce((s, r) => s + r.principal, 0), LOAN);
test('an even split falls five kobo short, so the last instalment carries them', [Math.floor(LOAN / MONTHS) * MONTHS, rows[0].principal, rows[6].principal], [11999995, 1714285, 1714290]);
test('interest is charged on what is outstanding, so it falls every month', [rows[0].interest, rows[6].interest, rows.every((r, i) => i === 0 || r.interest < rows[i - 1].interest)], [240000, 34286, true]);
test('the first month costs 2% of the whole loan', naira(rows[0].interest), '\u20a62,400.00');
test('the due dates keep the 31st where the month has one, and clamp where it does not', rows.map((r) => r.due), ['2026-01-31', '2026-02-28', '2026-03-31', '2026-04-30', '2026-05-31', '2026-06-30', '2026-07-31']);
test('every figure is a whole number of kobo, so nothing can drift', rows.every((r) => Number.isInteger(r.principal) && Number.isInteger(r.interest) && Number.isInteger(r.balance)), true);
test('each balance is the one above it less that month\u2019s principal', rows.every((r, i) => r.balance === (i === 0 ? LOAN : rows[i - 1].balance) - r.principal), true);
test('and the statement lines all come out the same width, ending at zero', [[...new Set(lines.map((l) => l.length))], lines[6].endsWith('\u20a60.00')], [[43], true]);
Everything in this section is here. Integer kobo so the totals close (exercise 63), Math.floor plus a remainder handed to the final row so the parts sum to the whole (exercise 82), the month-end clamp so a loan taken on the 31st does not fall into March (exercise 127), padStart columns so the statement reads (exercise 103), and thousands grouping only at the moment of printing (exercise 74).
The one design decision worth arguing about is where the remainder goes. ₦120,000 does not divide into seven equal kobo amounts — the even share is ₦1,714.285 and seven of those leave five kobo unaccounted for. Giving them to the last instalment keeps every earlier row identical and makes the final balance exactly zero, which is the property that matters: a schedule whose last line reads ₦0.05 outstanding will generate a dunning letter, a late fee, and a phone call. The alternative, spreading the five kobo over the first five rows, is equally correct and harder to explain to the customer.
Interest on the reducing balance, rather than a flat fee, is why the instalments are not equal: the first month charges 2% of ₦120,000 and the last charges 2% of what is nearly nothing. That is simple interest on the outstanding amount, which is not the same as the annuity formula a bank uses to make every payment identical — that needs powers and a proper rounding policy on the final row, and the honest version of it starts exactly here, with integers and a remainder that has somewhere to go.
Common mistakes
- Splitting with plain division.
12000000 / 7isn't a whole number of kobo. UseMath.floorand give the remainder to one row. - Adding months with
new Date(year, month + n, day)and no clamp. A due date on the 31st jumps from January into March. - Formatting too early. Do every calculation in kobo and turn it into naira only when printing.
Try it yourself
- Spread the 5 extra kobo over the first five rows, one kobo each, instead of putting them on the last row. Check that the balance still ends at zero.
- Add an interest column to the statement and keep every line the same width.
- Run the schedule for ₦50,000 over 4 months starting on 30 January 2026. What due date does February get?