mirror of
https://github.com/Raghu-Ch/ES6-Handson.git
synced 2026-02-10 04:33:02 -05:00
25 lines
1.1 KiB
JavaScript
25 lines
1.1 KiB
JavaScript
let calculateMonthlyPayment = function (principal, years, rate) {
|
|
let monthlyRate = 0;
|
|
if (rate) {
|
|
monthlyRate = rate / 100 / 12;
|
|
}
|
|
let monthlyPayment = principal * monthlyRate / (1 - (Math.pow(1 / (1 + monthlyRate), years * 12)));
|
|
return {principal, years, rate, monthlyPayment, monthlyRate};
|
|
// Creating Objects from Variables ## ES6
|
|
// shorted for the following ES5 syntax
|
|
// return { principal: principal,
|
|
// years: years,
|
|
// rate: rate,
|
|
// monthlyPayment: monthlyPayment,
|
|
// monthlyRate: monthlyRate };
|
|
};
|
|
|
|
document.getElementById('calcBtn').addEventListener('click', function () {
|
|
let principal = document.getElementById("principal").value;
|
|
let years = document.getElementById("years").value;
|
|
let rate = document.getElementById("rate").value;
|
|
let {monthlyPayment, monthlyRate} = calculateMonthlyPayment(principal, years, rate);
|
|
document.getElementById("monthlyPayment").innerHTML = monthlyPayment.toFixed(2);
|
|
document.getElementById("monthlyRate").innerHTML = (monthlyRate*100).toFixed(2);
|
|
});
|