Skip to content

Instantly share code, notes, and snippets.

@Trippnology
Forked from actuallymentor/compound-interest.js
Last active July 19, 2018 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Trippnology/7752a65fdead3c609085620a5a69077f to your computer and use it in GitHub Desktop.
Save Trippnology/7752a65fdead3c609085620a5a69077f to your computer and use it in GitHub Desktop.
A simple script to calculate compound interest
// Input it initial amount
// Interest as a number, e.g. 5% is 1.05 on a yearly basis
// Length as number of years
// Name of this calculation
// Addition determines whether the input variable is one time or a yearly contribution
function compound( input, interest, length, name, addition ) {
var accumulated = input
for ( i=0; i < length; i++ ) {
accumulated *= interest
if ( addition ){
accumulated += input
}
}
console.log( name + ' will '+(input > accumulated ? 'grow' : 'shrink')+' from ' + input + ' to ' + accumulated + ' at ' + interest + ' over ' + length + ' years' )
}
compound( 500, 1.05, 40, 'sigarettes', true ) // How much money will a habit of cigarettes generate in retirement money?
compound( 750, 1.05, 40, 'coffee', true ) // How much money will a habit of coffee outside the house generate in retirement money?
compound( 3750, 1.05, 40, 'lunch', true ) // How much money will a habit of lunch out the house generate in retirement money?
compound( 500, 1.05, 40, '500', false )
compound( 5000, 1.05, 40, '5000', false )
compound( 5000, 1.07, 40, '5000', false )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment