Last active
December 8, 2018 01:47
-
-
Save DJTB/d28013e6232852afca62af437d10cec3 to your computer and use it in GitHub Desktop.
compound-interest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function calc (P, PMT, r, n, t) { | |
const rn = (r / 100) / n | |
const nt = n * t | |
const pn = 12 / n | |
const principal = P * Math.pow(1 + rn, nt) | |
const series = PMT * (pn * (Math.pow(1 + rn, nt) - 1) / rn) | |
return parseInt(principal + series, 10) | |
} | |
module.exports = opts => { | |
if (typeof opts !== 'object') { | |
throw new Error(`Expected 'opts' to be an object, received ${typeof opts}.`) | |
} | |
return calc( | |
opts.initial, | |
opts.monthly, | |
opts.interest, | |
opts.compound, | |
opts.years | |
) | |
} | |
module.exports.verbose = opts => { | |
if (typeof opts !== 'object') { | |
throw new Error(`Expected 'opts' to be an object, received ${typeof opts}.`) | |
} | |
return Array(opts.years + 1) | |
.fill(null) | |
.map((x, idx) => | |
calc( | |
opts.initial, | |
opts.monthly, | |
opts.interest, | |
opts.compound, | |
idx | |
) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment