1function factorial(n) {
2 if (n < 0) return;
3 if (n < 2) return 1;
4 return n * factorial(n - 1);
5}
1function factorialize(num) {
2 if (num === 0 || num === 1)
3 return 1;
4 for (var i = num - 1; i >= 1; i--) {
5 num *= i;
6 }
7 return num;
8}
9factorialize(5);
1function factorialize(num) {
2 var result = num;
3 if (num === 0 || num === 1)
4 return 1;
5 while (num > 1) {
6 num--;
7 result *= num;
8 }
9 return result;
10}
1// Simple arrow function solution
2var factorial = n => (n <= 0) ? 1: n * factorial(n - 1);
1 function factorial(n) {
2 if (n > 1) {
3 return n * factorial(n - 1);
4 }
5 return 1;
6 }
1const factorial = n => n <= 1 ? 1 : n * factorial(n - 1);
2
3// Examples
4factorial(2); // 2
5factorial(3); // 6