My string is 10+20/3*87/234
how can I calculate the result without using eval function in javascript?
Asked
Active
Viewed 57 times
-1

Kiran
- 1
- 1
-
Possible duplicate of [Without using eval or a constructor function in Javascript , how can I calculate arithmetic in a given string](http://stackoverflow.com/q/31600121/1529630) – Oriol Mar 13 '16 at 05:23
-
Why not eval? Which is the syntax of your math expressions? – Oriol Mar 13 '16 at 05:28
1 Answers
0
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'));

Navoneel Talukdar
- 4,393
- 5
- 21
- 42