Created
April 11, 2022 22:03
-
-
Save petrbrzek/e17e8e97e1bca66ef57f12521988e4b1 to your computer and use it in GitHub Desktop.
Calculate compound interest with regular deposits and daily compounding period
This file contains hidden or 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
const calculateCompoundInterest = ({ | |
initialDeposit, | |
regularDeposit, | |
investmentInYears = 1, | |
depositFrequency = 12, | |
interestRate, | |
compoundPeriodsPerYear = 365 | |
}) => { | |
// https://www.vertex42.com/Calculators/compound-interest-calculator.html#rate-per-period | |
interestRate = interestRate / 100; | |
const totalCompoundingPeriods = depositFrequency * investmentInYears; | |
const rate = | |
Math.pow( | |
1 + interestRate / compoundPeriodsPerYear, | |
compoundPeriodsPerYear / depositFrequency | |
) - 1; | |
const futureValue = | |
initialDeposit * Math.pow(1 + rate, totalCompoundingPeriods) + | |
regularDeposit * ((Math.pow(1 + rate, totalCompoundingPeriods) - 1) / rate); | |
return { | |
futureValue, | |
interest: | |
futureValue - | |
initialDeposit - | |
regularDeposit * depositFrequency * investmentInYears, | |
totalDeposit: | |
initialDeposit + regularDeposit * depositFrequency * investmentInYears | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment