Skip to content

Instantly share code, notes, and snippets.

@willwillems
Last active July 2, 2018 17:56
Show Gist options
  • Save willwillems/27f6b323b055e45632cca9a5a270421e to your computer and use it in GitHub Desktop.
Save willwillems/27f6b323b055e45632cca9a5a270421e to your computer and use it in GitHub Desktop.
Calculates total savings due to compund interest based on a yearly rate and a monthly deposit.
// Calculates total savings as result of a monthly deposit.
// vars
var monthlySaving = 500; // monthly amount you deposit in your savings account/index fund
var initialSavings = 0; // initial capital you start with
var yearlyInterest = 1.05 // in this case %5
var years = 15; // amount of years to calculate for
var goal = 0 // financial goal, can leave at 0
// computed
var monthlyInterest = yearlyInterest**(1/12)
var total = initialSavings
for(i=0; i < (years*12); i++) {
// apply monthly interest and add new deposit to total
total = (total*monthlyInterest) + monthlySaving
// log current iteration and total
console.log(i + ': total: ' + total)
// if goal is reached log required time and break
if (goal && total>goal) { console.log('Goal reached in ' + ~~(i/12) + ' year(s) and ' + (i%12) + ' month(s).'); break; }
}
console.log('done!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment