CK WONG
CK WONG

Reputation: 81

how to simply use eval string formula with Abs() in js (google app script)

I see there is eval() to calculate string formula.

when i do eval("546+613-(30644-30600)-(789+834-(30665-30600))"), it works

but it failed for eval("546+613-ABS(30644-30600)-(789+834-ABS(30665-30600))").

So, I would ask if eval support abs()? and is there simply way to calculate the failed case?

Or, i have to Math.abs(), and calculate it piece by piece?

Thanks a lots! I am new to calculation world in JS.

Upvotes: 1

Views: 472

Answers (1)

SergGr
SergGr

Reputation: 23788

Using eval is usually a very bad idea in terms of security (because it does run arbitrary code rather than just an arithmetic expression) but if you want to use it, what's the problem with first replacing all "ABS" with "Math.abs" in the string before calling the eval?

Upvotes: 3

Related Questions