How the Loan is Calculated
The monthly payment is calculated based on the loan amount, interest rate, and loan term. The formula used is:
Monthly Payment = P × (r(1 + r)^n) / ((1 + r)^n – 1)
P = loan principal (amount), r = monthly interest rate, n = number of payments.
function calculateLoan() {
var loanAmount = parseFloat(document.getElementById(‘loanAmount’).value);
var loanTerm = parseInt(document.getElementById(‘loanTerm’).value);
var interestRate = parseFloat(document.getElementById(‘interestRate’).value) / 100 / 12;
if (isNaN(loanAmount) || isNaN(loanTerm) || isNaN(interestRate)) {
alert(‘Please enter valid values.’);
return;
}
var numberOfPayments = loanTerm * 12;
var monthlyPayment = loanAmount * interestRate * Math.pow(1 + interestRate, numberOfPayments) / (Math.pow(1 + interestRate, numberOfPayments) – 1);
document.getElementById(‘monthlyPayment’).value = monthlyPayment.toFixed(2);
}