mirror of
https://github.com/Raghu-Ch/ES6-Handson.git
synced 2026-02-10 04:33:02 -05:00
created mortgage2 for understanding ES6 class
This commit is contained in:
36
js/main.js
36
js/main.js
@@ -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
33
js/mortgage2.js
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user