Accumulated savings

2014-10-13

Little script to compute the result, including compound interest, on a savings account when you put in a certain amount every month. Of course this assumes the amount and interest rate remains stable throughout the period.

Code:
function f(monthPayment, yearInterest, years){
var monthInterest = Math.pow(yearInterest, 1/12);
var months = years * 12;

return monthPayment * monthInterest * (Math.pow(monthInterest, 120) - 1) / (monthInterest-1);
}

// 50..500 at 2% for 30 years:
f( 50, 1.02, 30); // 6640
f(100, 1.02, 30); // 13281
f(200, 1.02, 30); // 26563
f(500, 1.02, 30); // 66407

// 50..500 at 4% for 30 years:
f( 50, 1.04, 30); // 7358
f(100, 1.04, 30); // 14717
f(200, 1.04, 30); // 29435
f(500, 1.04, 30); // 73588

And this is how you compute the result from a starting capital without monthly input:

Code:
var capital = 500;
var interest = 1.02; // 2%
var years = 30;
var amount = capital * Math.pow(interest, years);
// 905