Skip to content

Instantly share code, notes, and snippets.

@DJTB
Last active December 8, 2018 01:47
Show Gist options
  • Save DJTB/d28013e6232852afca62af437d10cec3 to your computer and use it in GitHub Desktop.
Save DJTB/d28013e6232852afca62af437d10cec3 to your computer and use it in GitHub Desktop.
compound-interest
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