mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
chore: format code (#1515)
* chore: format code * Updated Documentation in README.md --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@ -6,53 +6,53 @@
|
||||
* @returns {number|null} - Result of the expression evaluation, or null if the expression is invalid.
|
||||
*/
|
||||
function evaluatePostfixExpression(expression) {
|
||||
const stack = [];
|
||||
const stack = []
|
||||
|
||||
// Helper function to perform an operation and push the result to the stack. Returns success.
|
||||
function performOperation(operator) {
|
||||
const rightOp = stack.pop(); // Right operand is the top of the stack
|
||||
const leftOp = stack.pop(); // Left operand is the next item on the stack
|
||||
const rightOp = stack.pop() // Right operand is the top of the stack
|
||||
const leftOp = stack.pop() // Left operand is the next item on the stack
|
||||
|
||||
if (leftOp === undefined || rightOp === undefined) {
|
||||
return false; // Invalid expression
|
||||
return false // Invalid expression
|
||||
}
|
||||
switch (operator) {
|
||||
case '+':
|
||||
stack.push(leftOp + rightOp);
|
||||
break;
|
||||
stack.push(leftOp + rightOp)
|
||||
break
|
||||
case '-':
|
||||
stack.push(leftOp - rightOp);
|
||||
break;
|
||||
stack.push(leftOp - rightOp)
|
||||
break
|
||||
case '*':
|
||||
stack.push(leftOp * rightOp);
|
||||
break;
|
||||
stack.push(leftOp * rightOp)
|
||||
break
|
||||
case '/':
|
||||
if (rightOp === 0) {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
stack.push(leftOp / rightOp);
|
||||
break;
|
||||
stack.push(leftOp / rightOp)
|
||||
break
|
||||
default:
|
||||
return false; // Unknown operator
|
||||
return false // Unknown operator
|
||||
}
|
||||
return true;
|
||||
return true
|
||||
}
|
||||
|
||||
const tokens = expression.split(/\s+/);
|
||||
const tokens = expression.split(/\s+/)
|
||||
|
||||
for (const token of tokens) {
|
||||
if (!isNaN(parseFloat(token))) {
|
||||
// If the token is a number, push it to the stack
|
||||
stack.push(parseFloat(token));
|
||||
stack.push(parseFloat(token))
|
||||
} else {
|
||||
// If the token is an operator, perform the operation
|
||||
if (!performOperation(token)) {
|
||||
return null; // Invalid expression
|
||||
return null // Invalid expression
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (stack.length === 1) ? stack[0] : null;
|
||||
return stack.length === 1 ? stack[0] : null
|
||||
}
|
||||
|
||||
export { evaluatePostfixExpression };
|
||||
export { evaluatePostfixExpression }
|
||||
|
@ -1,22 +1,21 @@
|
||||
import { evaluatePostfixExpression } from '../EvaluateExpression.js';
|
||||
import { evaluatePostfixExpression } from '../EvaluateExpression.js'
|
||||
|
||||
describe('evaluatePostfixExpression', () => {
|
||||
it('should evaluate a valid expression', () => {
|
||||
const expression = '3 4 * 2 / 5 +'; // (3 * 4) / 2 + 5 = 11
|
||||
const result = evaluatePostfixExpression(expression);
|
||||
expect(result).toBe(11);
|
||||
});
|
||||
const expression = '3 4 * 2 / 5 +' // (3 * 4) / 2 + 5 = 11
|
||||
const result = evaluatePostfixExpression(expression)
|
||||
expect(result).toBe(11)
|
||||
})
|
||||
|
||||
it('should handle division by zero', () => {
|
||||
const expression = '3 0 /'; // Division by zero
|
||||
const result = evaluatePostfixExpression(expression);
|
||||
expect(result).toBe(null);
|
||||
});
|
||||
const expression = '3 0 /' // Division by zero
|
||||
const result = evaluatePostfixExpression(expression)
|
||||
expect(result).toBe(null)
|
||||
})
|
||||
|
||||
it('should handle an invalid expression', () => {
|
||||
const expression = '3 * 4 2 / +'; // Invalid expression
|
||||
const result = evaluatePostfixExpression(expression);
|
||||
expect(result).toBe(null);
|
||||
});
|
||||
|
||||
});
|
||||
const expression = '3 * 4 2 / +' // Invalid expression
|
||||
const result = evaluatePostfixExpression(expression)
|
||||
expect(result).toBe(null)
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user