bagaimana menemukan faktorial nomor di javascript
function factorial(n) {
if (n < 0) return;
if (n < 2) return 1;
return n * factorial(n - 1);
}
TC5550
function factorial(n) {
if (n < 0) return;
if (n < 2) return 1;
return n * factorial(n - 1);
}
function factorialize(num) {
if(num < 2) return 1;
return num *= factorialize(num - 1);
}
function factorial(n) {
if (n == 1) {
return 1;
}
return n * factorial(n - 1);
}
console.log(factorial(6));
//Output: 720
function getFact(number) {
let fact = 1;
for (var i = 1; i <= number; i++) {
fact = fact * i;
}
return fact;
}
console.log(getFact(5))
//Output: 120
const factorial = n => n <= 1 ? 1 : n * factorial(n - 1);
// Examples
factorial(2); // 2
factorial(3); // 6
function factorial(num){
return num === 1 ? 1 : num * factorial(num - 1)
}