mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
added throwing an error when array with <3 items is passed
This commit is contained in:
@ -8,7 +8,7 @@
|
|||||||
export function maxProductOfThree(arrayItems) {
|
export function maxProductOfThree(arrayItems) {
|
||||||
// if size is less than 3, no triplet exists
|
// if size is less than 3, no triplet exists
|
||||||
let n = arrayItems.length
|
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],
|
let max1 = arrayItems[0],
|
||||||
max2 = -1,
|
max2 = -1,
|
||||||
max3 = -1,
|
max3 = -1,
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
import { maxProductOfThree } from '../MaxProductOfThree'
|
import { maxProductOfThree } from '../MaxProductOfThree'
|
||||||
|
|
||||||
describe('MaxProductOfThree', () => {
|
describe('MaxProductOfThree', () => {
|
||||||
it('expects to return -1 for array with only 2 numbers', () => {
|
it('expects to throw error for array with only 2 numbers', () => {
|
||||||
expect(maxProductOfThree([1, 3])).toBe(-1)
|
expect(() => {
|
||||||
|
maxProductOfThree([1, 3])
|
||||||
|
}).toThrow('Triplet cannot exist with the given array')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('expects to return 300 as the maximum product', () => {
|
it('expects to return 300 as the maximum product', () => {
|
||||||
|
Reference in New Issue
Block a user