const calcBtn =document.getElementById("submit");
const loan=document.getElementById("loan");
const interst2=document.getElementById("interest");
const term=document.getElementById("term");
// on hover button
calcBtn.addEventListener('mouseover', function(){
function delay(time) {
calcBtn.classList.add('animate__animated', 'animate__pulse');
return new Promise(resolve => setTimeout(resolve, time));
}
// remove class after settime
delay(1000).then(() => calcBtn.classList.remove('animate__animated', 'animate__pulse'));
});
calcBtn.addEventListener('click', function() {
// check to see if the full form is filled out
if(loan.value=="" | interst2.value=="" | term.value=="")
{
window.alert("Please fill out the complete form");
}
else{
CalculateMortgage(loan,interst2,term);
}
});
function CalculateMortgage(loan,interest2,term) {
let principal = parseFloat(loan.value);
let cInterest = parseFloat(interest2.value) / 100/ 12;
let cPayments = parseFloat(term.value);
let x = Math.pow(1+cInterest, cPayments);
let monthly = (principal * x * cInterest)/(x-1);
let total_payments = (monthly * cPayments).toFixed(2);
let total_interest_sum = ((monthly * cPayments)-principal).toFixed(2);
// define currency type
let formatCurrency = new Intl.NumberFormat(undefined, {
style: 'currency',
currency: 'USD'
});
let fmonthly = formatCurrency.format(monthly);
let fprincipal = formatCurrency.format(principal);
let ftotal_payments = formatCurrency.format(total_payments);
let ftotal_interest_sum = formatCurrency.format(total_interest_sum);
let info="";
// add info to table
info += "";
info += ""+fmonthly+" |
";
info += "Loan Amount: | ";
info += ""+fprincipal+" |
";
info += "Total Payments: | ";
info += ""+ftotal_payments+" |
";
info += "Total Interest Paid: | ";
info += ""+ftotal_interest_sum+" |
";
info += "Payments: | ";
info += ""+cPayments+" |
";
info += "
";
// display info to html
document.getElementById("loan_info").innerHTML = info;
// reset table
let table="";
table += "";
table += "";
table += " Month | ";
table += "Payment | ";
table += "Principle | ";
table += " Interest | ";
table += "Interest Paid | ";
table += "Balance | ";
table += "
";
// get input values
let current_balance = parseFloat(document.getElementById('loan').value);
let monthly_payment = monthly
// init counter
let payment_counter = 1;
let total_interest = parseFloat(cInterest);
while (payment_counter <= cPayments) {
towards_interest = cInterest * current_balance;
towards_balance = monthly_payment - towards_interest;
total_interest = total_interest + towards_interest;
current_balance = current_balance - towards_balance;
let ftowards_interest = formatCurrency.format(towards_interest);
let ftowards_balance = formatCurrency.format(towards_balance);
let ftotal_interest = formatCurrency.format(total_interest);
let fcurrent_balance = formatCurrency.format(current_balance);
let fmonthly_payment = formatCurrency.format(monthly_payment);
table += "";
table += ""+payment_counter+" | "
table += ""+fmonthly_payment+" | "
table += ""+ftowards_balance+" | "
table += ""+ftowards_interest+" | "
table += ""+ftotal_interest+" | "
table += ""+fcurrent_balance+" | "
table += "
";
payment_counter++;
}
table +="
";
document.getElementById("Schedule").innerHTML = table;
}