mirror of
https://github.com/Raghu-Ch/ES6-Handson.git
synced 2026-02-10 04:33:02 -05:00
Using classes in ES6
This commit is contained in:
@@ -73,58 +73,63 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
|
||||||
var calculateMonthlyPayment = function calculateMonthlyPayment(principal, years, rate) {
|
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
||||||
var monthlyRate = 0;
|
|
||||||
if (rate) {
|
|
||||||
monthlyRate = rate / 100 / 12;
|
|
||||||
}
|
|
||||||
var monthlyPayment = principal * monthlyRate / (1 - Math.pow(1 / (1 + monthlyRate), years * 12));
|
|
||||||
return { principal: principal, years: years, rate: rate, monthlyPayment: monthlyPayment, monthlyRate: monthlyRate };
|
|
||||||
// Creating Objects from Variables ## ES6
|
|
||||||
// shorted for the following ES5 syntax
|
|
||||||
// return { principal: principal,
|
|
||||||
// years: years,
|
|
||||||
// rate: rate,
|
|
||||||
// monthlyPayment: monthlyPayment,
|
|
||||||
// monthlyRate: monthlyRate };
|
|
||||||
};
|
|
||||||
var calculateAmortization = function calculateAmortization(principal, years, rate) {
|
|
||||||
var _calculateMonthlyPaym = calculateMonthlyPayment(principal, years, rate),
|
|
||||||
monthlyRate = _calculateMonthlyPaym.monthlyRate,
|
|
||||||
monthlyPayment = _calculateMonthlyPaym.monthlyPayment;
|
|
||||||
|
|
||||||
var balance = principal;
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||||
var amortization = [];
|
|
||||||
for (var y = 0; y < years; y++) {
|
var Mortgage = function () {
|
||||||
var interestY = 0; // Interest payment for year y
|
function Mortgage(principal, years, rate) {
|
||||||
var principalY = 0; // principal payment for year y
|
_classCallCheck(this, Mortgage);
|
||||||
|
|
||||||
|
this.principal = principal;
|
||||||
|
this.years = years;
|
||||||
|
this.rate = rate;
|
||||||
|
}
|
||||||
|
|
||||||
|
_createClass(Mortgage, [{
|
||||||
|
key: 'monthlyPayment',
|
||||||
|
get: function get() {
|
||||||
|
var monthlyRate = this.rate / 100 / 12;
|
||||||
|
return this.principal * monthlyRate / (1 - Math.pow(1 / (1 + monthlyRate), this.years * 12));
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
key: 'amortization',
|
||||||
|
get: function get() {
|
||||||
|
var monthlyPayment = this.monthlyPayment;
|
||||||
|
var monthlyRate = this.rate / 100 / 12;
|
||||||
|
var balance = this.principal;
|
||||||
|
var amortization = [];
|
||||||
|
for (var y = 0; y < this.years; y++) {
|
||||||
|
var interestY = 0;
|
||||||
|
var principalY = 0;
|
||||||
for (var m = 0; m < 12; m++) {
|
for (var m = 0; m < 12; m++) {
|
||||||
var interestM = balance * monthlyRate; // Interest payment for month m
|
var interestM = balance * monthlyRate;
|
||||||
var principalM = monthlyPayment - interestM; //principal payment for month m
|
var principalM = monthlyPayment - interestM;
|
||||||
interestY = interestY + interestM;
|
interestY = interestY + interestM;
|
||||||
principalY = principalY + principalM;
|
principalY = principalY + principalM;
|
||||||
balance = balance - principalM;
|
balance = balance - principalM;
|
||||||
}
|
}
|
||||||
amortization.push({ principalY: principalY, interestY: interestY, balance: balance });
|
amortization.push({ principalY: principalY, interestY: interestY, balance: balance });
|
||||||
|
}
|
||||||
|
return amortization;
|
||||||
}
|
}
|
||||||
return { monthlyPayment: monthlyPayment, monthlyRate: monthlyRate, amortization: amortization };
|
}]);
|
||||||
};
|
|
||||||
|
return Mortgage;
|
||||||
|
}();
|
||||||
|
|
||||||
document.getElementById('calcBtn').addEventListener('click', function () {
|
document.getElementById('calcBtn').addEventListener('click', function () {
|
||||||
var principal = document.getElementById("principal").value;
|
var principal = document.getElementById("principal").value;
|
||||||
var years = document.getElementById("years").value;
|
var years = document.getElementById("years").value;
|
||||||
var rate = document.getElementById("rate").value;
|
var rate = document.getElementById("rate").value;
|
||||||
|
var mortgage = new Mortgage(principal, years, rate);
|
||||||
var _calculateAmortizatio = calculateAmortization(principal, years, rate),
|
document.getElementById("monthlyPayment").innerHTML = mortgage.monthlyPayment.toFixed(2);
|
||||||
monthlyPayment = _calculateAmortizatio.monthlyPayment,
|
document.getElementById("monthlyRate").innerHTML = (rate / 12).toFixed(2);
|
||||||
monthlyRate = _calculateAmortizatio.monthlyRate,
|
var html = "";
|
||||||
amortization = _calculateAmortizatio.amortization;
|
mortgage.amortization.forEach(function (year, index) {
|
||||||
|
return html += '\n <tr>\n <td>' + (index + 1) + '</td>\n <td class="currency">' + Math.round(year.principalY) + '</td>\n <td class="stretch">\n <div class="flex">\n <div class="bar principal"\n style="flex:' + year.principalY + ';-webkit-flex:' + year.principalY + '">\n </div>\n <div class="bar interest"\n style="flex:' + year.interestY + ';-webkit-flex:' + year.interestY + '">\n </div>\n </div>\n </td>\n <td class="currency left">' + Math.round(year.interestY) + '</td>\n <td class="currency">' + Math.round(year.balance) + '</td>\n </tr>\n ';
|
||||||
document.getElementById("monthlyPayment").innerHTML = monthlyPayment.toFixed(2);
|
});
|
||||||
document.getElementById("monthlyRate").innerHTML = (monthlyRate * 100).toFixed(2);
|
document.getElementById("amortization").innerHTML = html;
|
||||||
amortization.forEach(function (month) {
|
|
||||||
return console.log(month);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/***/ })
|
/***/ })
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
92
js/main.js
92
js/main.js
@@ -1,43 +1,65 @@
|
|||||||
let calculateMonthlyPayment = (principal, years, rate) => {
|
class Mortgage {
|
||||||
let monthlyRate = 0;
|
constructor(principal, years, rate) {
|
||||||
if (rate) {
|
this.principal = principal;
|
||||||
monthlyRate = rate / 100 / 12;
|
this.years = years;
|
||||||
}
|
this.rate = rate;
|
||||||
let monthlyPayment = principal * monthlyRate / (1 - (Math.pow(1 / (1 + monthlyRate), years * 12)));
|
}
|
||||||
return {principal, years, rate, monthlyPayment, monthlyRate};
|
|
||||||
// Creating Objects from Variables ## ES6
|
get monthlyPayment() {
|
||||||
// shorted for the following ES5 syntax
|
let monthlyRate = this.rate / 100 / 12;
|
||||||
// return { principal: principal,
|
return this.principal * monthlyRate / (1 - (Math.pow(1/(1 + monthlyRate),
|
||||||
// years: years,
|
this.years * 12)));
|
||||||
// rate: rate,
|
}
|
||||||
// monthlyPayment: monthlyPayment,
|
|
||||||
// monthlyRate: monthlyRate };
|
get amortization() {
|
||||||
};
|
let monthlyPayment = this.monthlyPayment;
|
||||||
let calculateAmortization = (principal, years, rate) => {
|
let monthlyRate = this.rate / 100 / 12;
|
||||||
let {monthlyRate, monthlyPayment} = calculateMonthlyPayment(principal, years, rate);
|
let balance = this.principal;
|
||||||
let balance = principal;
|
|
||||||
let amortization = [];
|
let amortization = [];
|
||||||
for (let y=0; y<years; y++) {
|
for (let y=0; y<this.years; y++) {
|
||||||
let interestY = 0; // Interest payment for year y
|
let interestY = 0;
|
||||||
let principalY = 0; // principal payment for year y
|
let principalY = 0;
|
||||||
for (let m=0; m<12; m++) {
|
for (let m=0; m<12; m++) {
|
||||||
let interestM = balance * monthlyRate; // Interest payment for month m
|
let interestM = balance * monthlyRate;
|
||||||
let principalM = monthlyPayment - interestM; //principal payment for month m
|
let principalM = monthlyPayment - interestM;
|
||||||
interestY = interestY + interestM;
|
interestY = interestY + interestM;
|
||||||
principalY = principalY + principalM;
|
principalY = principalY + principalM;
|
||||||
balance = balance - principalM;
|
balance = balance - principalM;
|
||||||
}
|
}
|
||||||
amortization.push({principalY, interestY, balance})
|
amortization.push({principalY, interestY, balance});
|
||||||
}
|
}
|
||||||
return {monthlyPayment, monthlyRate, amortization};
|
return amortization;
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
document.getElementById('calcBtn').addEventListener('click', () => {
|
document.getElementById('calcBtn').addEventListener('click', () => {
|
||||||
let principal = document.getElementById("principal").value;
|
let principal = document.getElementById("principal").value;
|
||||||
let years = document.getElementById("years").value;
|
let years = document.getElementById("years").value;
|
||||||
let rate = document.getElementById("rate").value;
|
let rate = document.getElementById("rate").value;
|
||||||
let {monthlyPayment, monthlyRate, amortization} = calculateAmortization(principal, years, rate);
|
let mortgage = new Mortgage(principal, years, rate);
|
||||||
document.getElementById("monthlyPayment").innerHTML = monthlyPayment.toFixed(2);
|
document.getElementById("monthlyPayment").innerHTML = mortgage.monthlyPayment.toFixed(2);
|
||||||
document.getElementById("monthlyRate").innerHTML = (monthlyRate*100).toFixed(2);
|
document.getElementById("monthlyRate").innerHTML = (rate / 12).toFixed(2);
|
||||||
amortization.forEach(month => console.log(month));
|
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;
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
33
js/mortgage.js
Normal file
33
js/mortgage.js
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
export 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 };
|
||||||
|
};
|
||||||
|
export let calculateAmortization = (principal, years, rate) => {
|
||||||
|
let {monthlyRate, monthlyPayment} = calculateMonthlyPayment(principal, years, rate);
|
||||||
|
let balance = 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})
|
||||||
|
}
|
||||||
|
return {monthlyPayment, monthlyRate, amortization};
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user