Skip to content

Commit 9c790bc

Browse files
committed
fixes mentioned issues
1 parent 4fd461f commit 9c790bc

File tree

1 file changed

+46
-52
lines changed

1 file changed

+46
-52
lines changed

Data-Structures/Stack/EvaluateExpression.js

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,58 @@
22
* Evaluate a numeric operations string in postfix notation using a stack.
33
* Supports basic arithmetic operations: +, -, *, /
44
* @see https://www.geeksforgeeks.org/evaluation-of-postfix-expression/
5-
* @param {string} expression - Numeric operations expression to evaluate.
5+
* @param {string} expression - Numeric operations expression to evaluate. Must be a valid postfix expression.
66
* @returns {number|null} - Result of the expression evaluation, or null if the expression is invalid.
77
*/
88
function evaluatePostfixExpression(expression) {
9-
const stack = [];
10-
11-
// Helper function to perform an operation and push the result to the stack
12-
function performOperation(operator) {
13-
const operand2 = stack.pop();
14-
const operand1 = stack.pop();
15-
if (operator === '+' || operator === '-' || operator === '*' || operator === '/') {
16-
if (operand1 === undefined || operand2 === undefined) {
17-
return null; // Invalid expression
18-
}
19-
switch (operator) {
20-
case '+':
21-
stack.push(operand1 + operand2);
22-
break;
23-
case '-':
24-
stack.push(operand1 - operand2);
25-
break;
26-
case '*':
27-
stack.push(operand1 * operand2);
28-
break;
29-
case '/':
30-
if (operand2 === 0) {
31-
return null; // Division by zero
32-
}
33-
stack.push(operand1 / operand2);
34-
break;
35-
}
36-
} else {
37-
return null; // Invalid operator
38-
}
9+
const stack = [];
10+
11+
// Helper function to perform an operation and push the result to the stack
12+
function performOperation(operator) {
13+
const rightOp = stack.pop(); // Right operand is the top of the stack
14+
const leftOp = stack.pop(); // Left operand is the next item on the stack
15+
16+
if (leftOp === undefined || rightOp === undefined) {
17+
return false; // Invalid expression
3918
}
40-
41-
const tokens = expression.split(' ');
42-
43-
for (const token of tokens) {
44-
if (!isNaN(parseFloat(token))) {
45-
// If the token is a number, push it to the stack
46-
stack.push(parseFloat(token));
47-
} else {
48-
// If the token is an operator, perform the operation
49-
const result = performOperation(token);
50-
if (result === null) {
51-
return null; // Invalid expression
19+
switch (operator) {
20+
case '+':
21+
stack.push(leftOp + rightOp);
22+
break;
23+
case '-':
24+
stack.push(leftOp - rightOp);
25+
break;
26+
case '*':
27+
stack.push(leftOp * rightOp);
28+
break;
29+
case '/':
30+
if (rightOp === 0) {
31+
return false;
5232
}
53-
}
33+
stack.push(leftOp / rightOp);
34+
break;
35+
default:
36+
break;
5437
}
55-
56-
if (stack.length === 1) {
57-
// The final result should be on the stack
58-
return stack[0];
38+
39+
}
40+
41+
const tokens = expression.split(/\s+/);
42+
43+
for (const token of tokens) {
44+
if (!isNaN(parseFloat(token))) {
45+
// If the token is a number, push it to the stack
46+
stack.push(parseFloat(token));
5947
} else {
60-
return null; // Invalid expression
48+
// If the token is an operator, perform the operation
49+
const result = performOperation(token);
50+
if (result === false) {
51+
return null; // Invalid expression
52+
}
6153
}
6254
}
63-
64-
export { evaluatePostfixExpression };
65-
55+
56+
return (stack.length === 1) ? stack[0] : null;
57+
}
58+
59+
export { evaluatePostfixExpression };

0 commit comments

Comments
 (0)