Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active March 11, 2021 02:19
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 primaryobjects/2d451c0de5e8d123cb43bc839b1a555f to your computer and use it in GitHub Desktop.
Save primaryobjects/2d451c0de5e8d123cb43bc839b1a555f to your computer and use it in GitHub Desktop.
Future value calculator. FV=PV * ((1+r)^t), FV = future value, PV = present value, r = interest rate, t = compound time
const fv = (pv, r, t) => {
// FV = PV * ((1+r)^t)
return pv * Math.pow(1 + r, t);
}
const interest = (pv, fv, t) => {
// r = sqrt[t](FV / PV) - 1
return Math.pow(fv / pv, 1/t) - 1;
};
// What is the future value of $1,000 at an interest rate of 11% for 5 years?
console.log(fv(1000, 0.11, 5).toFixed(2));
console.log(fv(1000, 0.02, 5).toFixed(2));
// What is the interest rate after 5 years for a future value of $1,685.06 from a present value of $1,000?
console.log((interest(1000, 1685.06, 5) * 100).toFixed(2) + '%');
console.log((interest(1000, 1104.08, 5) * 100).toFixed(2) + '%');
1685.06
1104.08
11.00%
2.00%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment