Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5e1f46f

Browse files
committed
fix: Improve factorial implementation with input validation and base case optimization
1 parent d518fd0 commit 5e1f46f

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)

AltStyle によって変換されたページ (->オリジナル) /