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);
const n = loanTerm * 12;
const a = _.round(loanAmount * r * (1 + r)**n / ((1 + r)**n - 1), 2);
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];
}