Skip to content

Commit 5465fa0

Browse files
Update factorial
Add negative input validation to factorial implementation
2 parents d518fd0 + 5e1f46f commit 5465fa0

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ console.log(fibonacci(6)); // Output: 8
144144

145145
```js
146146
function factorial(n) {
147-
if (n === 0) return 1;
147+
if (n < 0) throw new RangeError('Factorial is not defined for negative numbers');
148+
149+
if (n === 0 || n === 1) return 1;
150+
148151
return n * factorial(n - 1);
149152
}
150153

0 commit comments

Comments
 (0)