I am currently working on an earnings calculator function
EarningsCalculator.prototype.calculateEarning = function (interestRate, investmentAmount) {
var earningHistory = {};
var currentInvestment = investmentAmount;
for (var year = 1; year <= 20; year++) {
earnedAmount = currentInvestment * interestRate;
currentInvestment = currentInvestment + earnedAmount;
if (year % 5 === 0 || year === 1) {
earningHistory[year] = currentInvestment;
}
}
return earningHistory;
}
This function takes an initial "investment amount" and calculates the earnings based on a given interest rate. The calculation is done for multiple years using a loop, but I want to modify it slightly. I need the earnings at 1-year intervals for the first year and then at 5-year intervals from year 5 onwards.
So with an initial investment of $1000 and an interest rate of 10%, the output for 20 years would look like this:
Year1 = $1100
Year5 = $1610.51
Year10 = $2593.74246
Year15 = $4177.248169
Year20 = $6727.499949