Reputation: 81
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
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