My string is 10+20/3*87/234 how can I calculate the result without using eval function in javascript?
-
Possible duplicate of Without using eval or a constructor function in Javascript , how can I calculate arithmetic in a given stringOriol– Oriol2016-03-13 05:23:41 +00:00Commented Mar 13, 2016 at 5:23
-
Why not eval? Which is the syntax of your math expressions?Oriol– Oriol2016-03-13 05:28:35 +00:00Commented Mar 13, 2016 at 5:28
Add a comment
|
1 Answer
You can use function constructor https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
function calculate(fn) {
return new Function('return ' + fn)();
}
alert(calculate('10+20/3*87/234'));