diff --git a/build/main.bundle.js b/build/main.bundle.js index e8015d2..c71cc17 100644 --- a/build/main.bundle.js +++ b/build/main.bundle.js @@ -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 \n ' + (index + 1) + '\n ' + Math.round(year.principalY) + '\n \n
\n
\n
\n
\n
\n
\n \n ' + Math.round(year.interestY) + '\n ' + Math.round(year.balance) + '\n \n '; + }); + document.getElementById("amortization").innerHTML = html; }); /***/ }) diff --git a/build/main.bundle.js.map b/build/main.bundle.js.map index 47c9fe8..6c331ba 100644 --- a/build/main.bundle.js.map +++ b/build/main.bundle.js.map @@ -1 +1 @@ -{"version":3,"sources":["webpack:///webpack/bootstrap 08364d4a2409405e582d","webpack:///./js/main.js"],"names":["calculateMonthlyPayment","principal","years","rate","monthlyRate","monthlyPayment","Math","pow","calculateAmortization","balance","amortization","y","interestY","principalY","m","interestM","principalM","push","document","getElementById","addEventListener","value","innerHTML","toFixed","forEach","console","log","month"],"mappings":";AAAA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;;AAGA;AACA;;AAEA;AACA;;AAEA;AACA,mDAA2C,cAAc;;AAEzD;AACA;AACA;AACA;AACA;AACA;AACA;AACA,aAAK;AACL;AACA;;AAEA;AACA;AACA;AACA,mCAA2B,0BAA0B,EAAE;AACvD,yCAAiC,eAAe;AAChD;AACA;AACA;;AAEA;AACA,8DAAsD,+DAA+D;;AAErH;AACA;;AAEA;AACA;;;;;;;;;;AChEA,IAAIA,0BAA0B,SAA1BA,uBAA0B,CAACC,SAAD,EAAYC,KAAZ,EAAmBC,IAAnB,EAA4B;AACtD,QAAIC,cAAc,CAAlB;AACA,QAAID,IAAJ,EAAU;AACNC,sBAAcD,OAAO,GAAP,GAAa,EAA3B;AACH;AACD,QAAIE,iBAAiBJ,YAAYG,WAAZ,IAA2B,IAAKE,KAAKC,GAAL,CAAS,KAAK,IAAIH,WAAT,CAAT,EAAgCF,QAAQ,EAAxC,CAAhC,CAArB;AACA,WAAO,EAACD,oBAAD,EAAYC,YAAZ,EAAmBC,UAAnB,EAAyBE,8BAAzB,EAAyCD,wBAAzC,EAAP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACH,CAdD;AAeA,IAAII,wBAAwB,SAAxBA,qBAAwB,CAACP,SAAD,EAAYC,KAAZ,EAAmBC,IAAnB,EAA4B;AAAA,gCAChBH,wBAAwBC,SAAxB,EAAmCC,KAAnC,EAA0CC,IAA1C,CADgB;AAAA,QAC/CC,WAD+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B,CAAvB,CAAtD;AACAL,aAASC,cAAT,CAAwB,aAAxB,EAAuCG,SAAvC,GAAmD,CAAClB,cAAY,GAAb,EAAkBmB,OAAlB,CAA0B,CAA1B,CAAnD;AACAb,iBAAac,OAAb,CAAqB;AAAA,eAASC,QAAQC,GAAR,CAAYC,KAAZ,CAAT;AAAA,KAArB;AACH,CARD,E","file":"main.bundle.js","sourcesContent":[" \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId])\n \t\t\treturn installedModules[moduleId].exports;\n\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// identity function for calling harmony imports with the correct context\n \t__webpack_require__.i = function(value) { return value; };\n\n \t// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, {\n \t\t\t\tconfigurable: false,\n \t\t\t\tenumerable: true,\n \t\t\t\tget: getter\n \t\t\t});\n \t\t}\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = 0);\n\n\n\n// WEBPACK FOOTER //\n// webpack/bootstrap 08364d4a2409405e582d","let calculateMonthlyPayment = (principal, years, rate) => {\r\n let monthlyRate = 0;\r\n if (rate) {\r\n monthlyRate = rate / 100 / 12;\r\n }\r\n let monthlyPayment = principal * monthlyRate / (1 - (Math.pow(1 / (1 + monthlyRate), years * 12)));\r\n return {principal, years, rate, monthlyPayment, monthlyRate};\r\n // Creating Objects from Variables ## ES6\r\n // shorted for the following ES5 syntax\r\n // return { principal: principal,\r\n // years: years,\r\n // rate: rate,\r\n // monthlyPayment: monthlyPayment,\r\n // monthlyRate: monthlyRate };\r\n};\r\nlet calculateAmortization = (principal, years, rate) => {\r\n let {monthlyRate, monthlyPayment} = calculateMonthlyPayment(principal, years, rate);\r\n let balance = principal;\r\n let amortization = [];\r\n for (let y=0; y {\r\n let principal = document.getElementById(\"principal\").value;\r\n let years = document.getElementById(\"years\").value;\r\n let rate = document.getElementById(\"rate\").value;\r\n let {monthlyPayment, monthlyRate, amortization} = calculateAmortization(principal, years, rate);\r\n document.getElementById(\"monthlyPayment\").innerHTML = monthlyPayment.toFixed(2);\r\n document.getElementById(\"monthlyRate\").innerHTML = (monthlyRate*100).toFixed(2);\r\n amortization.forEach(month => console.log(month));\r\n});\r\n\n\n\n// WEBPACK FOOTER //\n// ./js/main.js"],"sourceRoot":""} \ No newline at end of file +{"version":3,"sources":["webpack:///webpack/bootstrap 87a4a8b20aa950c121a1","webpack:///./js/main.js"],"names":["Mortgage","principal","years","rate","monthlyRate","Math","pow","monthlyPayment","balance","amortization","y","interestY","principalY","m","interestM","principalM","push","document","getElementById","addEventListener","value","mortgage","innerHTML","toFixed","html","forEach","year","index","round"],"mappings":";AAAA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;A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AaO,IAAb,CAAkB,EAACJ,sBAAD,EAAaD,oBAAb,EAAwBH,gBAAxB,EAAlB;AACD;AACD,aAAOC,YAAP;AACD;;;;;;AAKHQ,SAASC,cAAT,CAAwB,SAAxB,EAAmCC,gBAAnC,CAAoD,OAApD,EAA6D,YAAM;AAC/D,MAAIlB,YAAYgB,SAASC,cAAT,CAAwB,WAAxB,EAAqCE,KAArD;AACA,MAAIlB,QAAQe,SAASC,cAAT,CAAwB,OAAxB,EAAiCE,KAA7C;AACA,MAAIjB,OAAOc,SAASC,cAAT,CAAwB,MAAxB,EAAgCE,KAA3C;AACA,MAAIC,WAAW,IAAIrB,QAAJ,CAAaC,SAAb,EAAwBC,KAAxB,EAA+BC,IAA/B,CAAf;AACAc,WAASC,cAAT,CAAwB,gBAAxB,EAA0CI,SAA1C,GAAsDD,SAASd,cAAT,CAAwBgB,OAAxB,CAAgC,CAAhC,CAAtD;AACAN,WAASC,cAAT,CAAwB,aAAxB,EAAuCI,SAAvC,GAAmD,CAACnB,OAAO,EAAR,EAAYoB,OAAZ,CAAoB,CAApB,CAAnD;AACA,MAAIC,OAAO,EAAX;AACAH,WAASZ,YAAT,CAAsBgB,OAAtB,CAA8B,UAACC,IAAD,EAAOC,KAAP;AAAA,WAAiBH,8CAEjCG,QAAQ,CAFyB,iDAGhBtB,KAAKuB,KAAL,CAAWF,KAAKd,UAAhB,CAHgB,0KAOZc,KAAKd,UAPO,sBAOoBc,KAAKd,UAPzB,4HAUZc,KAAKf,SAVO,sBAUmBe,KAAKf,SAVxB,yHAcXN,KAAKuB,KAAL,CAAWF,KAAKf,SAAhB,CAdW,gDAehBN,KAAKuB,KAAL,CAAWF,KAAKlB,OAAhB,CAfgB,+BAAjB;AAAA,GAA9B;AAkBAS,WAASC,cAAT,CAAwB,cAAxB,EAAwCI,SAAxC,GAAoDE,IAApD;AAEH,CA5BD,E","file":"main.bundle.js","sourcesContent":[" \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId])\n \t\t\treturn installedModules[moduleId].exports;\n\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// identity function for calling harmony imports with the correct context\n \t__webpack_require__.i = function(value) { return value; };\n\n \t// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, {\n \t\t\t\tconfigurable: false,\n \t\t\t\tenumerable: true,\n \t\t\t\tget: getter\n \t\t\t});\n \t\t}\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = 0);\n\n\n\n// WEBPACK FOOTER //\n// webpack/bootstrap 87a4a8b20aa950c121a1","class Mortgage {\r\n constructor(principal, years, rate) {\r\n this.principal = principal;\r\n this.years = years;\r\n this.rate = rate;\r\n }\r\n\r\n get monthlyPayment() {\r\n let monthlyRate = this.rate / 100 / 12;\r\n return this.principal * monthlyRate / (1 - (Math.pow(1/(1 + monthlyRate),\r\n this.years * 12)));\r\n }\r\n\r\n get amortization() {\r\n let monthlyPayment = this.monthlyPayment;\r\n let monthlyRate = this.rate / 100 / 12;\r\n let balance = this.principal;\r\n let amortization = [];\r\n for (let y=0; y {\r\n let principal = document.getElementById(\"principal\").value;\r\n let years = document.getElementById(\"years\").value;\r\n let rate = document.getElementById(\"rate\").value;\r\n let mortgage = new Mortgage(principal, years, rate);\r\n document.getElementById(\"monthlyPayment\").innerHTML = mortgage.monthlyPayment.toFixed(2);\r\n document.getElementById(\"monthlyRate\").innerHTML = (rate / 12).toFixed(2);\r\n let html = \"\";\r\n mortgage.amortization.forEach((year, index) => html += `\r\n \r\n ${index + 1}\r\n ${Math.round(year.principalY)}\r\n \r\n
\r\n
\r\n
\r\n
\r\n
\r\n
\r\n \r\n ${Math.round(year.interestY)}\r\n ${Math.round(year.balance)}\r\n \r\n `);\r\n document.getElementById(\"amortization\").innerHTML = html;\r\n\r\n});\r\n\n\n\n// WEBPACK FOOTER //\n// ./js/main.js"],"sourceRoot":""} \ No newline at end of file diff --git a/js/main.js b/js/main.js index 1f4f907..0a6bfb7 100644 --- a/js/main.js +++ b/js/main.js @@ -1,43 +1,65 @@ -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 }; -}; -let calculateAmortization = (principal, years, rate) => { - let {monthlyRate, monthlyPayment} = calculateMonthlyPayment(principal, years, rate); - let balance = principal; +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 { let principal = document.getElementById("principal").value; let years = document.getElementById("years").value; let rate = document.getElementById("rate").value; - let {monthlyPayment, monthlyRate, amortization} = calculateAmortization(principal, years, rate); - document.getElementById("monthlyPayment").innerHTML = monthlyPayment.toFixed(2); - document.getElementById("monthlyRate").innerHTML = (monthlyRate*100).toFixed(2); - amortization.forEach(month => console.log(month)); + let mortgage = new Mortgage(principal, years, rate); + document.getElementById("monthlyPayment").innerHTML = mortgage.monthlyPayment.toFixed(2); + document.getElementById("monthlyRate").innerHTML = (rate / 12).toFixed(2); + let html = ""; + mortgage.amortization.forEach((year, index) => html += ` + + ${index + 1} + ${Math.round(year.principalY)} + +
+
+
+
+
+
+ + ${Math.round(year.interestY)} + ${Math.round(year.balance)} + + `); + document.getElementById("amortization").innerHTML = html; + }); diff --git a/js/mortgage.js b/js/mortgage.js new file mode 100644 index 0000000..8e5d4d4 --- /dev/null +++ b/js/mortgage.js @@ -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