Calculate Factorial
factorial
The factorial
function computes the factorial of a non-negative numeric value recursively.
Function Signature
factorial(int: Numeric | undefined): number | undefined
Parameters
int
: A numeric input value or numeric string whose factorial should be calculated. Can also beundefined
.
Return Value
- Returns the factorial result as a number if the input is valid.
- Returns
undefined
if the input is negative, not numeric, non-integer orundefined
.
Example Usage
import { factorial } from 'nhb-toolbox';
factorial(5); // 120
factorial(0); // 1
factorial(1); // 1
factorial(-3); // undefined
factorial(undefined); // undefined
factorial(5.5); // undefined
Aliases
factorial
can also be imported using the following aliases:
getFactorial
: Alias forfactorial
.calculateFactorial
: Alias forfactorial
.
Notes
- The function uses a recursive approach internally.
- Input is normalized via normalizeNumber before computation.
- Factorial of
0
and1
is1
. - It does not compute factorial for fractions (non-integer values).
- Can return very large values quickly due to the rapid growth rate of factorial values.
- Only non-negative integer values are considered valid for calculating factorial.
Conclusion
The factorial
function is useful for mathematical calculations involving permutations, combinations, and other scenarios where factorial values are required. It provides safe handling of invalid inputs by returning undefined
for negative numbers, non-numeric and non-integer values, or undefined
inputs.