I\'m trying to work on a script where the user inserts a monthly income and gets the future value with compound interest after 30 years. As it is now, I\'ve assigned some values
The Math.pow
is unnecessary, since you are calculating and incrementing futureValue
month by month. Simply multiply by 1 + monthlyRate
. You also want to add the current value of the investment to the new investment before multiplying:
for ( i = 1; i <= months; i++ ) {
futureValue = (futureValue + investment) * (1 + monthlyRate);
}
Alternatively, you can also calculate this in one go with the following formula:
futureValue = investment * (Math.pow(1 + monthlyRate, months) - 1) / monthlyRate;