I have a javascript / jquery code that takes in a formula and numbers from the user and solves the equation. My code in jquery can get the solution down to real numbers, but in the form of a string and I can’t get javascript to solve the equation mathematically.
I have something like
1000 * (200+3928)/2333
However, those numbers are all in string format and forcing each of them into floats or integers and reinserting them only turns them back into a string. Tried using valueOf and other things, but nothing works. Is there a better way or something I am missing?
Thanks
When you have a simple equation in the form of a string, the most practical method is to parse the string into
prefixorpostfixnotation, then evaluate it accordingly…Assuming the original string is
infix(the notation humans use to learn basic math) such as…1000 * (200+3928)/2333You then convert it to
postfixto obtain…1000, 200, 3928, +, *, 2333, /Using this notation, a computer can easily evaluate the expression using a simple loop and stack…
I wont post actual code, because I want to leave the fun parts up to you, but if you’d like a headstart with psuedo code, here goes…