mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 17:50:39 +08:00
feat: add algorithm to evaluate postfix string (#1441)
* feat: add algorithm to evaluate postfix strings * feat: add test case for evaluate expression * update: add literature reference * fix: import name in testcase * fix: test case result * Make clear that this is postfix * Update tests * add: see reference * fixes mentioned issues * Fix `default` case --------- Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
22
Data-Structures/Stack/test/EvaluateExpression.test.js
Normal file
22
Data-Structures/Stack/test/EvaluateExpression.test.js
Normal file
@ -0,0 +1,22 @@
|
||||
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);
|
||||
});
|
||||
|
||||
it('should handle division by zero', () => {
|
||||
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);
|
||||
});
|
||||
|
||||
});
|
Reference in New Issue
Block a user