Skip to content

Commit 67ec915

Browse files
committed
added throwing an error when array with <3 items is passed
1 parent b3377bb commit 67ec915

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

Dynamic-Programming/MaxProductOfThree.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
export function maxProductOfThree(arrayItems) {
99
// if size is less than 3, no triplet exists
1010
let n = arrayItems.length
11-
if (n < 3) return -1
11+
if (n < 3) throw new Error('Triplet cannot exist with the given array')
1212
let max1 = arrayItems[0],
1313
max2 = -1,
1414
max3 = -1,

Dynamic-Programming/tests/MaxProductOfThree.test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { maxProductOfThree } from '../MaxProductOfThree'
22

33
describe('MaxProductOfThree', () => {
4-
it('expects to return -1 for array with only 2 numbers', () => {
5-
expect(maxProductOfThree([1, 3])).toBe(-1)
4+
it('expects to throw error for array with only 2 numbers', () => {
5+
expect(() => {
6+
maxProductOfThree([1, 3])
7+
}).toThrow('Triplet cannot exist with the given array')
68
})
79

810
it('expects to return 300 as the maximum product', () => {

0 commit comments

Comments
 (0)