mirror of
https://github.com/Raghu-Ch/ES6-Handson.git
synced 2026-02-10 12:43:01 -05:00
Initial commit
This commit is contained in:
24
js/main.js
Normal file
24
js/main.js
Normal file
@@ -0,0 +1,24 @@
|
||||
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);
|
||||
});
|
||||
Reference in New Issue
Block a user