Skip to content

Instantly share code, notes, and snippets.

@ilonacodes
Created January 18, 2021 12:24
Show Gist options
  • Save ilonacodes/773c3e2c57ec41457bcb593ef4dd1fd6 to your computer and use it in GitHub Desktop.
Save ilonacodes/773c3e2c57ec41457bcb593ef4dd1fd6 to your computer and use it in GitHub Desktop.
// 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