From 430e7f3b7931ceed6c4aaa5b2d3ca6691235ee0e Mon Sep 17 00:00:00 2001 From: manish Date: Wed, 23 Oct 2019 23:27:18 +0530 Subject: [PATCH 1/2] postfix-expression-evaluation #61 - Update test case for exception --- .../postfix-expression-evaluation/index.js | 73 ++++++++++++------- .../postfix-expression-evaluation.test.js | 58 ++++++++------- 2 files changed, 78 insertions(+), 53 deletions(-) diff --git a/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js b/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js index 9db393e9..b1691654 100644 --- a/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js +++ b/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js @@ -6,39 +6,56 @@ const Stack = require('../index'); +const ERROR_STRING = 'Expression is not in order'; + function evaluatePostfixExpression(expression) { - let s = new Stack(); - for (let i = 0; i < expression.length; i++) { - const char = expression[i]; - if (!isNaN(char)) { - //if number push the char onto stack - s.push(Number(char)); - } else { - // if char is an operator then pop two elements from stack, evaluate them accordingly based on operator. - //push the result to stack - let val1 = s.pop(); - let val2 = s.pop(); - switch (char) { - case '+': - s.push(val2 + val1); - break; - case '-': - s.push(val2 - val1); - break; - case '*': - s.push(val2 * val1); - break; - case '/': - s.push(val2 / val1); - break; + // eslint-disable-next-line no-param-reassign + expression = expression.trim(); + + if (expression.length === 0 || expression.length === 1) { + return ERROR_STRING; + } - } - } + const s = new Stack(); + // eslint-disable-next-line no-plusplus + for (let i = 0; i < expression.length; i++) { + const char = expression[i]; + // eslint-disable-next-line no-restricted-globals + if (!isNaN(char)) { + // if number push the char onto stack + s.push(Number(char)); + } else { + // if char is an operator then pop two elements from stack, evaluate them accordingly based on operator. + // push the result to stack + const val1 = s.pop(); + const val2 = s.pop(); + switch (char) { + case '+': + s.push(val2 + val1); + break; + case '-': + s.push(val2 - val1); + break; + case '*': + s.push(val2 * val1); + break; + case '/': + s.push(val2 / val1); + break; + default: + break; + } } - //pop the value of postfix expression - return s.pop(); + } + // pop the value from stack + const result = s.pop(); + if (s.isEmpty()) { + return result; + } + return ERROR_STRING; } module.exports = { evaluatePostfixExpression, + ERROR_STRING, }; diff --git a/src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js b/src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js index 47e0de42..ee12ce8e 100644 --- a/src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js +++ b/src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js @@ -1,55 +1,63 @@ -const { evaluatePostfixExpression } = require('.'); +const { evaluatePostfixExpression, ERROR_STRING } = require('.'); -describe('Postfix expression evaluation', function () { - it('should be a function', function () { +describe('Postfix expression evaluation', () => { + it('should be a function', () => { expect(typeof evaluatePostfixExpression).toEqual('function'); }); - - it('should return a number', function () { + + it('should return a number', () => { const expression = '11+'; - - expect(typeof evaluatePostfixExpression(expression)).toEqual('number') + + expect(typeof evaluatePostfixExpression(expression)).toEqual('number'); }); - - it('should handle addition', function () { + + it('should handle addition', () => { const expression = '23+'; const expected = 5; - + expect(evaluatePostfixExpression(expression)).toEqual(expected); }); - - it('should handle subtraction', function () { + + it('should handle subtraction', () => { const expression = '54-'; const expected = 1; - + expect(evaluatePostfixExpression(expression)).toEqual(expected); }); - - it('should handle multiplication', function () { + + it('should handle multiplication', () => { const expression = '34*'; const expected = 12; - + expect(evaluatePostfixExpression(expression)).toEqual(expected); }); - - it('should handle division', function () { + + it('should handle division', () => { const expression = '62/'; const expected = 3; - + expect(evaluatePostfixExpression(expression)).toEqual(expected); }); - - it('should handle negative numbers', function () { + + it('should handle negative numbers', () => { const expression = '25-'; const expected = -3; - + expect(evaluatePostfixExpression(expression)).toEqual(expected); }); - - it('should handle multiple operators', function () { + + it('should handle multiple operators', () => { const expression = '123*+'; const expected = 7; - + expect(evaluatePostfixExpression(expression)).toEqual(expected); }); + + describe('should throw error on invalid expressions', () => { + const invalidExpressions = ['12', '1', '+', '1+2', '+12']; + test.each(invalidExpressions)('running for %p', (expression) => { + const result = evaluatePostfixExpression(expression); + expect(result).toEqual(ERROR_STRING); + }); + }); }); From 46b2fc786ef11524a07962acbe0379085d13afaa Mon Sep 17 00:00:00 2001 From: manish Date: Thu, 24 Oct 2019 00:09:40 +0530 Subject: [PATCH 2/2] thow error instead of string error message --- .../Stack/postfix-expression-evaluation/index.js | 4 ++-- .../postfix-expression-evaluation.test.js | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js b/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js index b1691654..0a683aae 100644 --- a/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js +++ b/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js @@ -13,7 +13,7 @@ function evaluatePostfixExpression(expression) { expression = expression.trim(); if (expression.length === 0 || expression.length === 1) { - return ERROR_STRING; + throw new Error(ERROR_STRING); } const s = new Stack(); @@ -52,7 +52,7 @@ function evaluatePostfixExpression(expression) { if (s.isEmpty()) { return result; } - return ERROR_STRING; + throw new Error(ERROR_STRING); } module.exports = { diff --git a/src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js b/src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js index ee12ce8e..25da462f 100644 --- a/src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js +++ b/src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js @@ -56,8 +56,7 @@ describe('Postfix expression evaluation', () => { describe('should throw error on invalid expressions', () => { const invalidExpressions = ['12', '1', '+', '1+2', '+12']; test.each(invalidExpressions)('running for %p', (expression) => { - const result = evaluatePostfixExpression(expression); - expect(result).toEqual(ERROR_STRING); + expect(() => evaluatePostfixExpression(expression)).toThrow(ERROR_STRING); }); }); });