forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorial.js
More file actions
Latest commit
23 lines (19 loc) · 524 Bytes
/
Copy pathFactorial.js
File metadata and controls
23 lines (19 loc) · 524 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* @function Factorial
* @description function to find factorial using recursion.
* @param {Integer} n - The input integer
* @return {Integer} - Factorial of n.
* @see [Factorial](https://en.wikipedia.org/wiki/Factorial)
* @example 5! = 1*2*3*4*5 = 120
* @example 2! = 1*2 = 2
*/
constfactorial=(n)=>{
if(!Number.isInteger(n)||n<0){
thrownewRangeError('Input should be a non-negative whole number')
}
if(n===0){
return1
}
returnn*factorial(n-1)
}
export{factorial}