mirror of
https://github.com/Raghu-Ch/ES6-Handson.git
synced 2026-02-10 12:43:01 -05:00
Using classes in ES6
This commit is contained in:
92
js/main.js
92
js/main.js
@@ -1,43 +1,65 @@
|
||||
let calculateMonthlyPayment = (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 };
|
||||
};
|
||||
let calculateAmortization = (principal, years, rate) => {
|
||||
let {monthlyRate, monthlyPayment} = calculateMonthlyPayment(principal, years, rate);
|
||||
let balance = principal;
|
||||
class Mortgage {
|
||||
constructor(principal, years, rate) {
|
||||
this.principal = principal;
|
||||
this.years = years;
|
||||
this.rate = rate;
|
||||
}
|
||||
|
||||
get monthlyPayment() {
|
||||
let monthlyRate = this.rate / 100 / 12;
|
||||
return this.principal * monthlyRate / (1 - (Math.pow(1/(1 + monthlyRate),
|
||||
this.years * 12)));
|
||||
}
|
||||
|
||||
get amortization() {
|
||||
let monthlyPayment = this.monthlyPayment;
|
||||
let monthlyRate = this.rate / 100 / 12;
|
||||
let balance = this.principal;
|
||||
let amortization = [];
|
||||
for (let y=0; y<years; y++) {
|
||||
let interestY = 0; // Interest payment for year y
|
||||
let principalY = 0; // principal payment for year y
|
||||
for (let m=0; m<12; m++) {
|
||||
let interestM = balance * monthlyRate; // Interest payment for month m
|
||||
let principalM = monthlyPayment - interestM; //principal payment for month m
|
||||
interestY = interestY + interestM;
|
||||
principalY = principalY + principalM;
|
||||
balance = balance - principalM;
|
||||
}
|
||||
amortization.push({principalY, interestY, balance})
|
||||
for (let y=0; y<this.years; y++) {
|
||||
let interestY = 0;
|
||||
let principalY = 0;
|
||||
for (let m=0; m<12; m++) {
|
||||
let interestM = balance * monthlyRate;
|
||||
let principalM = monthlyPayment - interestM;
|
||||
interestY = interestY + interestM;
|
||||
principalY = principalY + principalM;
|
||||
balance = balance - principalM;
|
||||
}
|
||||
amortization.push({principalY, interestY, balance});
|
||||
}
|
||||
return {monthlyPayment, monthlyRate, amortization};
|
||||
};
|
||||
return amortization;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
document.getElementById('calcBtn').addEventListener('click', () => {
|
||||
let principal = document.getElementById("principal").value;
|
||||
let years = document.getElementById("years").value;
|
||||
let rate = document.getElementById("rate").value;
|
||||
let {monthlyPayment, monthlyRate, amortization} = calculateAmortization(principal, years, rate);
|
||||
document.getElementById("monthlyPayment").innerHTML = monthlyPayment.toFixed(2);
|
||||
document.getElementById("monthlyRate").innerHTML = (monthlyRate*100).toFixed(2);
|
||||
amortization.forEach(month => console.log(month));
|
||||
let mortgage = new Mortgage(principal, years, rate);
|
||||
document.getElementById("monthlyPayment").innerHTML = mortgage.monthlyPayment.toFixed(2);
|
||||
document.getElementById("monthlyRate").innerHTML = (rate / 12).toFixed(2);
|
||||
let html = "";
|
||||
mortgage.amortization.forEach((year, index) => html += `
|
||||
<tr>
|
||||
<td>${index + 1}</td>
|
||||
<td class="currency">${Math.round(year.principalY)}</td>
|
||||
<td class="stretch">
|
||||
<div class="flex">
|
||||
<div class="bar principal"
|
||||
style="flex:${year.principalY};-webkit-flex:${year.principalY}">
|
||||
</div>
|
||||
<div class="bar interest"
|
||||
style="flex:${year.interestY};-webkit-flex:${year.interestY}">
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<td class="currency left">${Math.round(year.interestY)}</td>
|
||||
<td class="currency">${Math.round(year.balance)}</td>
|
||||
</tr>
|
||||
`);
|
||||
document.getElementById("amortization").innerHTML = html;
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user