Skip to content

Instantly share code, notes, and snippets.

@paulswail
Last active June 7, 2018 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulswail/016c6049dac3d8bd1f094f0fe0aeb63d to your computer and use it in GitHub Desktop.
Save paulswail/016c6049dac3d8bd1f094f0fe0aeb63d to your computer and use it in GitHub Desktop.
Calculate investment's compound growth
// Calculates the compound growth of an investment over number of periods.
// Assumes fixed regular contributions and average growth rate.
function compoundGrowth(startingBalance = 0, periodContribution = 100, periodGrowthRate=0, nPeriods = 1, currencyLabel = '£') {
const totalContribution = startingBalance + periodContribution * nPeriods;
const finalBalance = [...Array(nPeriods)].reduce((runningBalance) => {
return runningBalance + (runningBalance * periodGrowthRate) + periodContribution;
}, startingBalance);
const absoluteGrowth = finalBalance - totalContribution;
const relativeGrowth = absoluteGrowth / totalContribution;
console.log(`${currencyLabel}${finalBalance.toFixed(2)} will be returned after ${nPeriods} periods with ${currencyLabel}${periodContribution.toFixed(2)} contributed each period and an average period growth rate of ${periodGrowthRate * 100}% and initial lump sum of ${currencyLabel}${startingBalance}.
Absolute Growth: ${currencyLabel}${absoluteGrowth.toFixed(2)}
Relative Growth: ${(relativeGrowth * 100).toFixed(2)}%`);
return finalBalance;
}
compoundGrowth(0, 100, 0.005, 12);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment