Using classes in ES6

This commit is contained in:
2017-02-14 00:07:26 -08:00
parent 0ddde3bf1b
commit 2017cce5af
4 changed files with 141 additions and 81 deletions

View File

@@ -73,58 +73,63 @@
"use strict";
var calculateMonthlyPayment = function calculateMonthlyPayment(principal, years, rate) {
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 _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 balance = principal;
var amortization = [];
for (var y = 0; y < years; y++) {
var interestY = 0; // Interest payment for year y
var principalY = 0; // principal payment for year y
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Mortgage = function () {
function Mortgage(principal, years, rate) {
_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++) {
var interestM = balance * monthlyRate; // Interest payment for month m
var principalM = monthlyPayment - interestM; //principal payment for month m
interestY = interestY + interestM;
principalY = principalY + principalM;
balance = balance - principalM;
var interestM = balance * monthlyRate;
var principalM = monthlyPayment - interestM;
interestY = interestY + interestM;
principalY = principalY + principalM;
balance = balance - principalM;
}
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 () {
var principal = document.getElementById("principal").value;
var years = document.getElementById("years").value;
var rate = document.getElementById("rate").value;
var _calculateAmortizatio = calculateAmortization(principal, years, rate),
monthlyPayment = _calculateAmortizatio.monthlyPayment,
monthlyRate = _calculateAmortizatio.monthlyRate,
amortization = _calculateAmortizatio.amortization;
document.getElementById("monthlyPayment").innerHTML = monthlyPayment.toFixed(2);
document.getElementById("monthlyRate").innerHTML = (monthlyRate * 100).toFixed(2);
amortization.forEach(function (month) {
return console.log(month);
});
var principal = document.getElementById("principal").value;
var years = document.getElementById("years").value;
var rate = document.getElementById("rate").value;
var mortgage = new Mortgage(principal, years, rate);
document.getElementById("monthlyPayment").innerHTML = mortgage.monthlyPayment.toFixed(2);
document.getElementById("monthlyRate").innerHTML = (rate / 12).toFixed(2);
var html = "";
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("amortization").innerHTML = html;
});
/***/ })

File diff suppressed because one or more lines are too long