How To Convert Array To A Mathematical Computation Expression?
I tried to write a computation mathematical expression and store each sign into an array. For example, I want to convert the array: let arr = ['10', '-', '(', '2', '+', '1', ')', '
Solution 1:
It will not validate your input but eval(arr.join(""))
will do it.
eval
takes a string and executes it as it were a js expression.
Solution 2:
It work only with
eval
,
new Function
is not working for this issue
const arr = ['10', '-', '(', '2', '+', '1', ')', '*', '3'];
eval(arr.join(' ')); // 1
Post a Comment for "How To Convert Array To A Mathematical Computation Expression?"