From 67ec915d97a85ca17bb6c063e7b88ee1ce92206e Mon Sep 17 00:00:00 2001 From: devcer Date: Fri, 15 Oct 2021 21:49:29 +0530 Subject: [PATCH] added throwing an error when array with <3 items is passed --- Dynamic-Programming/MaxProductOfThree.js | 2 +- Dynamic-Programming/tests/MaxProductOfThree.test.js | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Dynamic-Programming/MaxProductOfThree.js b/Dynamic-Programming/MaxProductOfThree.js index 811913c69..1e7ebe3e1 100644 --- a/Dynamic-Programming/MaxProductOfThree.js +++ b/Dynamic-Programming/MaxProductOfThree.js @@ -8,7 +8,7 @@ export function maxProductOfThree(arrayItems) { // if size is less than 3, no triplet exists let n = arrayItems.length - if (n < 3) return -1 + if (n < 3) throw new Error('Triplet cannot exist with the given array') let max1 = arrayItems[0], max2 = -1, max3 = -1, diff --git a/Dynamic-Programming/tests/MaxProductOfThree.test.js b/Dynamic-Programming/tests/MaxProductOfThree.test.js index 41861bac1..32aaec542 100644 --- a/Dynamic-Programming/tests/MaxProductOfThree.test.js +++ b/Dynamic-Programming/tests/MaxProductOfThree.test.js @@ -1,8 +1,10 @@ import { maxProductOfThree } from '../MaxProductOfThree' describe('MaxProductOfThree', () => { - it('expects to return -1 for array with only 2 numbers', () => { - expect(maxProductOfThree([1, 3])).toBe(-1) + it('expects to throw error for array with only 2 numbers', () => { + expect(() => { + maxProductOfThree([1, 3]) + }).toThrow('Triplet cannot exist with the given array') }) it('expects to return 300 as the maximum product', () => {