Public
Edited
Aug 18, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = calculateLoanSchedule()
Insert cell
function calculateLoanSchedule() {
const startDate = new Date();
const endDate = (new Date(startDate)).setMonth(startDate.getMonth() + loanTerm * 12);

let balance = loanAmount;
let currentDate = startDate;
let data = [];
let totalInterest = 0;

const r = interestRate / (12*100); // monthly interest rate
const n = loanTerm * 12; // total number of payment
const a = _.round(loanAmount * r * (1 + r)**n / ((1 + r)**n - 1), 2); // amortized payment amount

while (currentDate < endDate) {
const interest = _.round(balance * r , 2);
const principal = _.round(a - interest, 2);
balance -= interest;
totalInterest += interest;
data.push({
date: new Date(currentDate),
payment: a,
principal: principal,
interest: interest,
totalInterest: totalInterest,
balance: balance,
});
currentDate.setMonth(currentDate.getMonth() + 1);
}
return [data, endDate, a, totalInterest];
}
Insert cell
_ = require('lodash')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more