Evaluate an Expression
Hard · Parsing
Write a function evaluate that calculates the result of a math expression given as a string. Do not use eval or Function.
- Numbers are non-negative integers (possibly with several digits).
- Operators: +, -, *, / and parentheses. * and / bind stronger than + and -; operators of the same strength go left to right.
- A minus sign can also negate a value, like "-(2 + 3)" or "2 * -3".
- Division truncates toward zero (use Math.trunc).
- Spaces can appear anywhere. The expression is always valid.