created mortgage2 for understanding ES6 class

This commit is contained in:
2017-02-14 00:17:59 -08:00
parent 2017cce5af
commit 74146a17ee
4 changed files with 68 additions and 50 deletions

View File

@@ -1,38 +1,4 @@
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<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 amortization;
}
}
import Mortgage from './mortgage2';
document.getElementById('calcBtn').addEventListener('click', () => {
let principal = document.getElementById("principal").value;

33
js/mortgage2.js Normal file
View File

@@ -0,0 +1,33 @@
export default 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<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 amortization;
}
}