From 192c0534a62f1bb57a16a94df7daeb55cda25348 Mon Sep 17 00:00:00 2001 From: akshat Date: Sun, 19 Jul 2026 12:32:05 +0530 Subject: [PATCH] fix: validate negative input in factorialRecursive function - Add input validation to throw an error for negative numbers - Update JSDoc to document the thrown error - Prevent incorrect behavior where negative inputs would return 1 --- src/algorithms/math/factorial/factorialRecursive.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/algorithms/math/factorial/factorialRecursive.js b/src/algorithms/math/factorial/factorialRecursive.js index e2b4aec6c9..c62a64f841 100644 --- a/src/algorithms/math/factorial/factorialRecursive.js +++ b/src/algorithms/math/factorial/factorialRecursive.js @@ -1,7 +1,11 @@ /** * @param {number} number * @return {number} + * @throws {Error} Throws an error if the number is negative */ export default function factorialRecursive(number) { + if (number < 0) { + throw new Error('Factorial is not defined for negative numbers'); + } return number > 1 ? number * factorialRecursive(number - 1) : 1; }