JavaScript Expression Evaluator
Develop a robust JavaScript function that can parse and evaluate mathematical expressions. This is a fundamental problem in computer science, with applications ranging from scientific calculators and spreadsheet software to programming language interpreters. The goal is to create a flexible and reliable way to compute the result of a given mathematical string.
Problem Description
Your task is to implement a JavaScript function, evaluateExpression(expression), that takes a string expression as input and returns its numerical result. The expression will consist of integers, basic arithmetic operators (+, -, *, /), and parentheses. You need to handle operator precedence (multiplication and division before addition and subtraction) and the correct evaluation of expressions within parentheses.
Key Requirements:
- Supported Operators: +, -, *, /
- Integer Operands: All numbers in the expression will be integers.
- Operator Precedence: Multiplication and division should be evaluated before addition and subtraction.
- Parentheses: Expressions within parentheses must be evaluated first, respecting nested parentheses.
- Whitespace: The input expression may contain whitespace, which should be ignored.
- Valid Expressions: Assume the input expression is always syntactically valid (e.g., no mismatched parentheses, valid operator/operand combinations).
- Division: For simplicity, assume division will always result in a whole number (no floating-point division or remainders).
Expected Behavior:
The function should return a single number representing the computed value of the expression.
Edge Cases to Consider:
- Expressions with only a single number.
- Expressions with leading/trailing whitespace.
- Expressions with multiple sets of parentheses.
- Expressions where operators and operands are adjacent with no whitespace.
Examples
Example 1:
Input: "3 + 5 * 2"
Output: 13
Explanation: Multiplication has higher precedence, so 5 * 2 = 10. Then, 3 + 10 = 13.
Example 2:
Input: "(3 + 5) * 2"
Output: 16
Explanation: The expression inside the parentheses is evaluated first: 3 + 5 = 8. Then, 8 * 2 = 16.
Example 3:
Input: "10 / 2 - 3 + (4 * 2)"
Output: 10
Explanation:
1. Parentheses: 4 * 2 = 8. Expression becomes "10 / 2 - 3 + 8".
2. Division: 10 / 2 = 5. Expression becomes "5 - 3 + 8".
3. Addition/Subtraction (left to right): 5 - 3 = 2. Expression becomes "2 + 8".
4. Final addition: 2 + 8 = 10.
Example 4:
Input: " 12 "
Output: 12
Explanation: Whitespace is ignored, and the expression is simply the number 12.
Constraints
- The input expression will contain only integers (positive), '+', '-', '*', '/', '(', ')', and whitespace characters.
- The length of the expression string will be between 1 and 1000 characters, inclusive.
- The intermediate and final results of calculations will fit within standard JavaScript number types.
- Division operations will always yield an integer result.
Notes
Consider using a two-stack approach (one for operands and one for operators) or a recursive approach to handle operator precedence and parentheses. Think about how to process the expression from left to right while respecting the order of operations. Remember to handle whitespace appropriately by either trimming it upfront or ignoring it during parsing.