Write a function to evaluate a mathematical expression string (e.g., "3 + 2 * (4 - 1)").
HardProblem Statement
Write a function to evaluate a mathematical expression string (e.g., "3 + 2 * (4 - 1)").
Sample Input
"3 + 2 * (4 - 1)"
Sample Output
9
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
// Evaluate a mathematical expression from a string
function evaluateExpression(expr) {
// Use Function constructor to evaluate the string as code
}
console.log(evaluateExpression("3 + 2 * (4 - 1)"));
Input
"3 + 2 * (4 - 1)"
Expected Output
9