function calculateEMISchedule(loanAmount, maxTenure, repaymentFrequency, firstEMIDate) {
const monthlyInterestRate = 0.01;
if (repaymentFrequency === 'days') {
maxTenure /= 30;
} else if (repaymentFrequency === 'weeks') {
maxTenure /= 4;
}
const interestFactor = Math.pow(1 + monthlyInterestRate, maxTenure);
const emi = (loanAmount * monthlyInterestRate * interestFactor) / (interestFactor - 1);
const emiSchedule = [];
let currentDate = new Date(firstEMIDate);
let remainingLoanAmount = loanAmount;
for (let i = 0; i < maxTenure; i++) {
const interestPayment = remainingLoanAmount * monthlyInterestRate;
const principalPayment = emi - interestPayment;
emiSchedule.push({
date: currentDate.toDateString(),
emiAmount: parseFloat(emi.toFixed(2)),
principalPayment: parseFloat(principalPayment.toFixed(2)),
interestPayment: parseFloat(interestPayment.toFixed(2)),
remainingLoanAmount: parseFloat((remainingLoanAmount - principalPayment).toFixed(2)),
});
if (repaymentFrequency === 'months') {
currentDate.setMonth(currentDate.getMonth() + 1);
} else if (repaymentFrequency === 'weeks') {
currentDate.setDate(currentDate.getDate() + 7);
}
remainingLoanAmount -= principalPayment;
}
return emiSchedule;
}
const loanAmount = 10000;
const maxTenureInWeeks = 52;
const repaymentFrequency = 'weeks';
const firstEMIDate = '2023-11-01';
const emiSchedule = calculateEMISchedule(loanAmount, maxTenureInWeeks, repaymentFrequency, firstEMIDate);
console.log(emiSchedule);