xxxxxxxxxx
function factorial(n) {
if (n == 1) {
return 1;
}
return n * factorial(n - 1);
}
console.log(factorial(6));
//Output: 720
xxxxxxxxxx
function factorial(n) {
if (n < 0) return;
if (n < 2) return 1;
return n * factorial(n - 1);
}
xxxxxxxxxx
function getFact(number) {
let fact = 1;
for (var i = 1; i <= number; i++) {
fact = fact * i;
}
return fact;
}
console.log(getFact(5))
//Output: 120
xxxxxxxxxx
function factorial(n) {
if (n > 1) {
return n * factorial(n - 1);
}
return 1;
}
xxxxxxxxxx
<script>
// Javascript to find factorial
// of given number
// function to find factorial
// of given number
function factorial(n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
// Driver Code
let num = 5;
document.write("Factorial of " + num + " is " + factorial(num));
</script>
xxxxxxxxxx
function factorialize(num) {
var factode = 1;
for(var i = num; i > 0; i--) {
factode *= i;
}
return factode;
}
factorialize(5);
xxxxxxxxxx
const factorialize = num => {
if (num === 0 || num === 1) {
return 1;
}
for (let i = num - 1; i >= 1; i--) {
num *= i;
}
return num;
};
xxxxxxxxxx
// Simple arrow function solution
var factorial = n => (n <= 0) ? 1: n * factorial(n - 1);
xxxxxxxxxx
function factorial(n) {
if (n == 1) {
return 1;
}
return n * factorial(n - 1);
}
console.log(factorial(6));
xxxxxxxxxx
function factorialize(num) {
if(num < 2) return 1;
return num *= factorialize(num - 1);
}