Odd Javascript Situation: array of numbers to comma separated list? Not to a string. For financejs

http://financejs.org/#IRR

Finance.js and its IRR helper takes this:

// e.g., If initial investment is -$500,000 and the cash flows are $200,000, $300,000, and $200,000, IRR is 18.82%.
// finance.IRR(initialInvestment, cashFlow1, cashFlow2, etc. );

  finance.IRR(-500000, 200000, 300000, 200000);
  => 18.82

I’m stumped on why it would be the above signature instead of using a array like below:

  finance.IRR(-500000, [ 200000, 300000, 200000 ]);
  => 18.82

Right now I have two variables:

originalInvestment (a number), and
payoutList (an array of numbers)

I frankly have no clue how to turn the payoutList array into what I need for IRR. I can’t pass the array in. I can’t pass a string with the numbers separated by commas… the majority of array methods either return an array or a string. How can I get to this!!!:

  finance.IRR(-500000, 200000, 300000, 200000);
  => 18.82

Does

finance.IRR.apply(this, array);

work?

When I do this:

finance.IRR(originalInvestment, arrayOfPayouts);

I get this:

Exception while invoking method 'Investments.getIRR' Error: IRR requires at least one positive value and one negative value

When I do this:

finance.IRR.apply(originalInvestment, arrayOfPayouts);

I get this:

Exception while invoking method 'Investments.getIRR' Error: IRR requires at least one positive value and one negative value

when I do this:

		let originalInvestment = 5000;
		let listOfAmounts = [1000, 2000, 3000];
		const pos_to_neg = (num) =>  -Math.abs(num); // turn original investment into a negative number
		let IRR = finance.IRR.apply(this, listOfAmounts.unshift(pos_to_neg(originalInvestment))); //push original investment to front of array and run financejs IRR

I get this:

Exception while invoking method 'Investments.getIRR' TypeError: Function.prototype.apply: Arguments list has wrong type

http://stackoverflow.com/questions/36922252/array-as-a-cashflow-financejs-npv

I see I missed a parameter.

So you need to ensure the originalInvestment is at element 0, with the remaining parameters following on. So, perhaps

listofAmount.splice(0, 0, originalInvestment);
let IRR = finance.IRR.apply(this, listofAmount);
1 Like

it works it works it works

1 Like