From f5e4733e1f599116c858417cc19df55e44b6b58d Mon Sep 17 00:00:00 2001 From: "eugene.yurchenko" Date: Wed, 4 Feb 2026 22:54:56 +0200 Subject: [PATCH] test: add edge case for factorial (negative) --- src/algorithms/math/factorial/__test__/factorial.test.js | 4 ++++ src/algorithms/math/factorial/factorial.js | 3 +++ 2 files changed, 7 insertions(+) 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) {