About
The factorial of a non-negative integer x is equal to the product of all integers less than or equal to x and greater than zero.
For example: factorial(4) would equal 4 * 3 * 2 * 1, which is 24.
In factorial, things are NOT repeated.
Number of combination from a set
It's used to calculate the number of combination in a set that permits to get the chance or probability.
If you have 3 elements [ a b c ], you have 6 possibilities or factorial 3:
- a b c
- a c b
- b a c
- b c a
- c a b
- c b a
You have therefore 1 change over 6 to pick the good combination.
Implementation
let factorial = function (n){
if(n === 0 || n === 1){
return 1;
} else {
return n * factorial(n-1);
}
}
console.log(factorial(3));