+ 1

how to change string that contain math operation and simbol into solveable statement in JavaScript

For example : Input : var a = "(" var b = "2" var c = "+" var d = "5" var e = ")" var f = [a,b,c,d,e] document.write(f.join("")) And the output is "(2+5)" But i want to make that the output is 7... I have tried using parseInt(f.join("")) but the result still "(2+5)" Which function should i use to solve those following string, thanks

15th Dec 2020, 4:49 PM
Bee David
Bee David - avatar
2 Réponses
+ 1
There is no javascript function that parses the entire expression at once. This is not an easy task that requires writing code. https://medium.com/@stoopidguy1992/how-to-write-a-math-expression-parser-in-javascript-b5147bc9466b
15th Dec 2020, 5:06 PM
Максим
Максим - avatar
+ 1
function adding(x,y){ var x1= parseInt(x); var y1 = parseInt(y); return x1+y1; }
15th Dec 2020, 5:22 PM
Shadoff
Shadoff - avatar