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

@@ -63,7 +63,7 @@
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ return __webpack_require__(__webpack_require__.s = 1);
/******/ })
/************************************************************************/
/******/ ([
@@ -73,6 +73,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
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; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
@@ -87,13 +91,13 @@ var Mortgage = function () {
}
_createClass(Mortgage, [{
key: 'monthlyPayment',
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',
key: "amortization",
get: function get() {
var monthlyPayment = this.monthlyPayment;
var monthlyRate = this.rate / 100 / 12;
@@ -118,11 +122,26 @@ var Mortgage = function () {
return Mortgage;
}();
exports.default = Mortgage;
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
var _mortgage = __webpack_require__(0);
var _mortgage2 = _interopRequireDefault(_mortgage);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 mortgage = new Mortgage(principal, years, rate);
var mortgage = new _mortgage2.default(principal, years, rate);
document.getElementById("monthlyPayment").innerHTML = mortgage.monthlyPayment.toFixed(2);
document.getElementById("monthlyRate").innerHTML = (rate / 12).toFixed(2);
var html = "";

File diff suppressed because one or more lines are too long

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;
}
}