Skip to content

Instantly share code, notes, and snippets.

@ghalimi
Last active March 31, 2017 23:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ghalimi/4597900 to your computer and use it in GitHub Desktop.
Save ghalimi/4597900 to your computer and use it in GitHub Desktop.
NPV Function
// Copyright (c) 2012 Sutoiku, Inc. (MIT License)
function NPV() {
// Cast arguments to array
var args = [];
for (i = 0; i < arguments.length; i++) {
args = args.concat(arguments[i]);
}
// Lookup rate
var rate = args[0];
// Initialize net present value
var value = 0;
// Loop on all values
for (var j = 1; j < args.length; j++) {
value += args[j] / Math.pow(1 + rate, j);
}
// Return net present value
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment