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 }
1// program to find the factorial of a number
2
3// take input from the user
4const number = parseInt(prompt('Enter a positive integer: '));
5
6// checking if number is negative
7if (number < 0) {
8 console.log('Error! Factorial for negative number does not exist.');
9}
10
11// if number is 0
12else if (number === 0) {
13 console.log(`The factorial of ${number} is 1.`);
14}
15
16// if number is positive
17else {
18 let fact = 1;
19 for (i = 1; i <= number; i++) {
20 fact *= i;
21 }
22 console.log(`The factorial of ${number} is ${fact}.`);
23}
24
25//output
26Enter a positive integer: 5
27The factorial of 5 is 120.