TV-C-15 TV-C-15 - 6 months ago 26
Javascript Question

Convert Excel math to JavaScript - trouble with ABS(Excel) to Modulus(JavaScript)

I have been converting an Excel spreadsheet to a JavaScript form all day.

It has gone pretty well. I have most of them working just fine.

I have been thrown by this one though:

EXCEL FORMULA:

G20*((G33-0.015)^(G17-G16))*((1-G33^(ABS(G17-G16)))/(1-G33))))


My JavaScript conversion:

var calc = tax_per_year*(Math.pow((inflation - 0.015), (career_start-age)))*((1 - Math.pow(inflation, (%(career_start - age)))/(1 - inflation)));


It seems that JavaScript does not like the placement of the converted Excel "ABS" converted to JavaScript "%" Modulus

Am I using this modulus correctly here?
I have tried a variety of placements.

Answer

Using modulo in this scenario is wrong and makes no sense in this case. Modulo delivers you the rest of a division. Just use the javascript abs function.

Math.abs(<your_value>);

Or have a look at the W3C Reference. Math.abs (W3C Reference)

Comments