1

I am get a formula to calculate the Net Present Value using Rent, Number of periods, Discount Rate and Future Value.I am able to get the Present Value however, I need a formula to calculate the Net Present Value on today's date or any date a user inputs. My code is as below:

function PV() {
  var future = 5000;
  var type = 1;
  periods = 12;
  rate = document.getElementById("rate").value;
  var ratePercent = periods * 100;
  rate = Math.pow(1 + rate / 100, 1 / 365) - 1;

  rate = eval(rate);
  periods = eval(periods);

  // Return present value
  if (rate === 0) {
    document.getElementById("presentResultValue").value = -payment * periods - future;
  } else {
    document.getElementById("presentResultValue").value = (
      (((1 - Math.pow(1 + rate, periods)) / rate) * payment * (1 + rate * type) - future) /
      Math.pow(1 + rate, periods)
    ).toFixed(2);
  }
}

I am also using Excel to calculate this but need a way to convert it to Javascript. I am also attaching my work with excel. ExcelNPV I am still learning JavaScript so any help will be greatly appreciated. Thank you.

4
  • 3
    JavaScript is not Java Mar 14, 2018 at 14:31
  • 1
    I think Google is better suited to give you this formula. Mar 14, 2018 at 14:33
  • @ScottMarcus Thank you. I tried google but they have different variables and i am just confused on how i can use the exact Excel i have and thought that maybe Stack would be of more help to me.
    – ace
    Mar 14, 2018 at 14:37
  • @UnholySheep Yes i know. I am trying to get a formula regardless of what technology i am using.
    – ace
    Mar 14, 2018 at 14:38

3 Answers 3

2

Came across NPV formula on my project and had to create a function for it. Finance.js is great but I didn't want to clutter my code with it because my problem was just getting this formula.

ES5 Function

/**
 * Calculates the Net Present Value of a given initial investment
 * cost and an array of cash flow values with the specified discount rate.
 *
 * @param {number} rate - The discount rate percentage
 * @param {number} initialCost - The initial investment
 * @param {array} cashFlows - An array of future payment amounts
 * @return {number} The calculated Net Present Value
 */
function getNPV(rate, initialCost, cashFlows) {
  var npv = initialCost;

  for (var i = 0; i < cashFlows.length; i++) {
    npv += cashFlows[i] / Math.pow(rate / 100 + 1, i + 1);
  }

  return npv;
}

Using the function:

var rate = 10;
var initialCost = -25000;
var cashFlows = [-10000, 0, 10000, 30000, 100000];

console.log(getNPV(rate, initialCost, cashFlows));
// expected output: 56004.77488497429

JavaScript ES6 Function:

https://www.evermade.fi/net-present-value-npv-formula-for-javascript/

/**
 * Calculates the Net Present Value of a given initial investment
 * cost and an array of cash flow values with the specified discount rate.
 * @param {number} rate - The discount rate percentage
 * @param {number} initialCost - The initial investment
 * @param {array} cashFlows - An array of future payment amounts
 * @return {number} The calculated Net Present Value
 */
function getNPV(rate, initialCost, cashFlows) {
  return cashFlows.reduce(
    (accumulator, currentValue, index) =>
      accumulator + currentValue / Math.pow(rate / 100 + 1, index + 1),
    initialCost
  );
}

I've explained most of it on my blogpost.

Hope this helps :)

0

One big thing you are missing with this is the cost. You have periods, you have rate, you have rate percent. But there is nothing there for the "cost".

You say you have this in Excel, and other languages. If you understand the calculations in Excel, then you may understand how you should do it in JavaScript.

In Excel you use the nifty function =NPV(.... where it takes 2 arguments. The first is the "RATE" and the second is the "VALUE" which can be multiple values. When you do this in Excel, one of the values you pass in would be the total cost. So you have something like this....

=NPV(2%,[total cost],[Year1Value],[Year2Value],[Year3Value],....) or =NPV(A1,A2:A12)

The "Total Cost" would go where you spend the money....assuming its at the end of Year 1, it would go before the "value" / return from year one.

With that being said, another thing to consider is determining WHEN the cost was needed.
If the cost is upfront, then it would be taken out of the "array" of values section and added to the NPV calculations such as....

=NPV(A1,A3:A12) + A2

Where cell A2 is the Cost (upfront) and A1 is the rate and A3-A12 are all the returns.

As a simple example, if you have the rate somewhere on the page, you can loop through the arguments that are going to be passed to it such as below....

function NPV() {
  var args = [];
  for (i = 0; i < arguments.length; i++) {
    args = args.concat(arguments[i]);
  }

  var rate = document.getElementById("rate").value;
  var value = 0;

  // loop through each argument, and calculate the value 
  // based on the rate, and add it to the "value" variable 
  // creating a running total
  for (var i = 1; i < args.length; i++) {
    value = value + ((args[i])/(Math.pow(1 + rate, i)));
  }

  return value;
}

Additionally, you could also look for a library such as Finanace.JS http://financejs.org/

0

This worked for me

function getNPV(rate, a, payment) {
        var npv = 0;
        for (var i = 0; i < a.length; i++) {
            npv += payment / Math.pow(1 + rate, a[i]);
            console.log(payment / Math.pow(1 + rate, a[i]))
        }
        return npv;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.