From 84c2d5b951605afd38897fd842162f2ed004f7f0 Mon Sep 17 00:00:00 2001 From: BENZABOSS Date: Sat, 14 Dec 2019 16:33:45 -0500 Subject: [PATCH 1/2] Add retirement calc solution --- retirementCalc.html | 11 ++++++++++ retirementCalc.js | 52 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 retirementCalc.html create mode 100644 retirementCalc.js diff --git a/retirementCalc.html b/retirementCalc.html new file mode 100644 index 0000000..20dcabf --- /dev/null +++ b/retirementCalc.html @@ -0,0 +1,11 @@ + + + + + Project Shift + + + + + + \ No newline at end of file diff --git a/retirementCalc.js b/retirementCalc.js new file mode 100644 index 0000000..86a4295 --- /dev/null +++ b/retirementCalc.js @@ -0,0 +1,52 @@ +// Developer Name: Ben Joyce +// Project Name: Retirement Calculator +// Date: 12/10/2019 + + + +const calcFunction = function(percentOfSalary, averageReturnRate, averageSalary, years) { + + + //Variables Declared + const percentARR = averageReturnRate * 100; + const pPOS = percentOfSalary * 100; + const taxRate = 0.25; + const lessTaxRate = 1 - taxRate; + const salaryToSave = averageSalary * lessTaxRate * percentOfSalary; + let total = 0; + + + + //function that calulates the balance at the end of one year + + const calcEndBalance = function (toSave, interest) { + const interestMultiplier = (1 + interest); + + for (let i = 0; i < years; i++ ) { + if (i > 0) { + total *= interestMultiplier; + } + + total += toSave; + console.log(total); + }; + + + return total.toFixed(2); + + }; + + const endBalance = calcEndBalance(salaryToSave, averageReturnRate); + + + + console.log(`With an average salary of $${averageSalary} over the next ${years} years, you will have $${endBalance} if you invest ${pPOS}% of your post-tax income with an ${percentARR}% return.`) +}; + + + + +calcFunction(.20, .08, 50000, 20); + + + From 86fd92616e9d5f7401c14478409cbf1063ff48fa Mon Sep 17 00:00:00 2001 From: BENZABOSS Date: Sat, 14 Dec 2019 17:14:15 -0500 Subject: [PATCH 2/2] Add thousands separator --- retirementCalc.js | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/retirementCalc.js b/retirementCalc.js index 86a4295..4e129d0 100644 --- a/retirementCalc.js +++ b/retirementCalc.js @@ -4,7 +4,7 @@ -const calcFunction = function(percentOfSalary, averageReturnRate, averageSalary, years) { +const calcFunction = function (percentOfSalary, averageReturnRate, averageSalary, years) { //Variables Declared @@ -22,28 +22,37 @@ const calcFunction = function(percentOfSalary, averageReturnRate, averageSalary, const calcEndBalance = function (toSave, interest) { const interestMultiplier = (1 + interest); - for (let i = 0; i < years; i++ ) { + for (let i = 0; i < years; i++) { if (i > 0) { total *= interestMultiplier; } total += toSave; - console.log(total); + }; - - - return total.toFixed(2); - - }; - - const endBalance = calcEndBalance(salaryToSave, averageReturnRate); + return total; + }; - console.log(`With an average salary of $${averageSalary} over the next ${years} years, you will have $${endBalance} if you invest ${pPOS}% of your post-tax income with an ${percentARR}% return.`) + // IIFE: set equal to a function being invoked (sets a variable that requires multiple operations) + const endBalance = function() { + let balance = calcEndBalance(salaryToSave, averageReturnRate); + balance = Math.round(balance); + balance = thousands_separators(balance); + return balance; + }(); + + function thousands_separators(num) { + var num_parts = num.toString().split("."); + num_parts[0] = num_parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","); + return num_parts.join("."); + } + + console.log(`With an average salary of $${thousands_separators(averageSalary)} over the next ${years} years, you will have $${endBalance} if you invest ${pPOS}% of your post-tax income with an ${percentARR}% return.`) }; - + calcFunction(.20, .08, 50000, 20);