mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-19 10:05:41 +08:00

* 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>
23 lines
733 B
JavaScript
23 lines
733 B
JavaScript
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);
|
|
});
|
|
|
|
});
|