Created
January 18, 2021 12:24
-
-
Save ilonacodes/773c3e2c57ec41457bcb593ef4dd1fd6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this function will return a table with data for all next years | |
const calculate = ({ principal, years, interest }) => { | |
const rate = interest / 100; | |
// the first entry is year 0—makes the logic simpler below | |
const table = [[0, principal, principal, 0]]; | |
for (let i = 1; i <= years; i++) { | |
table.push([ | |
i, // year number | |
table[i - 1][1] + principal * rate, // with simple interest | |
table[i - 1][2] * (1 + rate), // with compounding interest | |
table[i - 1][2] * rate // compounding dividends this year | |
]); | |
} | |
// remove convenience entry for year 0 | |
return table.slice(1); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment