Student Loan Interest Calculator
function calculateLoan() {
const loanAmount = parseFloat(document.getElementById(“loanAmount”).value);
const interestRate = parseFloat(document.getElementById(“interestRate”).value) / 100;
const loanTerm = parseInt(document.getElementById(“loanTerm”).value);
if (isNaN(loanAmount) || isNaN(interestRate) || isNaN(loanTerm) || loanAmount <= 0 || interestRate <= 0 || loanTerm <= 0) {
alert("Please enter valid positive values for all fields.");
return;
}
const monthlyRate = interestRate / 12;
const numberOfPayments = loanTerm * 12;
const monthlyPayment = (loanAmount * monthlyRate) / (1 – Math.pow(1 + monthlyRate, -numberOfPayments));
const totalPaid = monthlyPayment * numberOfPayments;
const totalInterest = totalPaid – loanAmount;
document.getElementById("monthlyPayment").value = monthlyPayment.toFixed(2);
document.getElementById("totalInterest").value = totalInterest.toFixed(2);
document.getElementById("totalAmount").value = totalPaid.toFixed(2);
}