diff --git a/src/algorithms/math/factorial/__test__/factorial.test.js b/src/algorithms/math/factorial/__test__/factorial.test.js index bf6aa0ecbd..cee9e6b5f1 100644 --- a/src/algorithms/math/factorial/__test__/factorial.test.js +++ b/src/algorithms/math/factorial/__test__/factorial.test.js @@ -8,4 +8,8 @@ describe('factorial', () => { expect(factorial(8)).toBe(40320); expect(factorial(10)).toBe(3628800); }); + + it('factorial of negative should throw', () => { + expect(() => factorial(-1)).toThrow(); + }); }); diff --git a/src/algorithms/math/factorial/factorial.js b/src/algorithms/math/factorial/factorial.js index 6c717d051f..9730d4f4f5 100644 --- a/src/algorithms/math/factorial/factorial.js +++ b/src/algorithms/math/factorial/factorial.js @@ -3,6 +3,9 @@ * @return {number} */ export default function factorial(number) { + if (number < 0) { + throw new RangeError('Number must be greater than 0'); + } let result = 1; for (let i = 2; i <= number; i += 1) {