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

Add negative input validation to factorial implementation #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
AllThingsSmitty merged 1 commit into AllThingsSmitty:main from alphamyd:fix/add-negative-input-validation-to-factorial-implementation
Jun 3, 2025
Merged

Add negative input validation to factorial implementation #5

AllThingsSmitty merged 1 commit into AllThingsSmitty:main from alphamyd:fix/add-negative-input-validation-to-factorial-implementation
Jun 3, 2025

Conversation

Copy link
Contributor

@alphamyd alphamyd commented May 31, 2025

This PR improves the existing factorial(n) function by adding input validation for negative numbers. Previously, calling factorial(-1) or any negative input would cause infinite recursion, eventually leading to a stack overflow. With this update, a RangeError is thrown when a negative number is passed.

Why this change is important:

  • Correctness: Mathematically, factorial is not defined for negative numbers.
  • Robustness: Prevents infinite recursion and potential stack overflow on invalid input.
  • Interview relevance: Input validation is a common area of oversight in candidate solutions. This change aligns the function with real-world expectations.
  • Clarity and semantics: Using RangeError instead of a generic Error makes it immediately clear that the issue is with the range of acceptable values, not a syntax issue or unexpected type. According to MDN, RangeError is appropriate when a value is not within the set or range of allowed values — which fits this case perfectly.
factorial(-5); // throws RangeError: Factorial is not defined for negative numbers
factorial(5); // returns 120

The updated function stays concise and educational while being safer and more professional.

Copy link
Contributor Author

I also updated the base case to handle both 0 and 1 directly, which avoids an unnecessary recursive call when n === 1. It’s a small optimization, but it keeps the call stack cleaner and better matches the mathematical definition.

Copy link
Contributor Author

alphamyd commented Jun 3, 2025

Copy link
Owner

@AllThingsSmitty AllThingsSmitty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ship it

alphamyd reacted with rocket emoji
@AllThingsSmitty AllThingsSmitty merged commit 5465fa0 into AllThingsSmitty:main Jun 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@AllThingsSmitty AllThingsSmitty AllThingsSmitty left review comments

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

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