Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solve an arithmetic string in javascript?

suppose I have the following code:

    var str = "4*(3)^2/1"

Is the simplest solution just to make a stack of the operators and solve with postfix notation? Or is there a really basic solution I'm missing.

Additionally how can I adapt if I'm using log, ln, sin, cos, and tan?

like image 892
Jamie Alizadeh Avatar asked May 19 '26 09:05

Jamie Alizadeh


1 Answers

Sorry to respond to my own question, but the easiest solution is using math.js

    var ans = math.eval(str);
like image 92
Jamie Alizadeh Avatar answered May 20 '26 22:05

Jamie Alizadeh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!