Answers for "how to find factorial of a number in javascript"

19

how to find factorial of a number in javascript

function factorial(n) {
  if (n < 0) return;
  if (n < 2) return 1;
  return n * factorial(n - 1);
}
Posted by: Guest on May-24-2020
1

javascript factorial

function factorialize(num) {
  if (num === 0 || num === 1)
    return 1;
  for (var i = num - 1; i >= 1; i--) {
    num *= i;
  }
  return num;
}
factorialize(5);
Posted by: Guest on November-25-2019
1

javascript factorial of a number

const factorial = n => n <= 1 ? 1 : n * factorial(n - 1);

// Examples
factorial(2);   // 2
factorial(3);   // 6
Posted by: Guest on July-03-2020
0

factorial javascript function

function factorialize(num) {
  if(num < 2) return 1;
  return num *= factorialize(num - 1);
}
Posted by: Guest on July-24-2020
0

factorial in javascript

// program to find the factorial of a number

// take input from the user
const number = parseInt(prompt('Enter a positive integer: '));

// checking if number is negative
if (number < 0) {
    console.log('Error! Factorial for negative number does not exist.');
}

// if number is 0
else if (number === 0) {
    console.log(`The factorial of ${number} is 1.`);
}

// if number is positive
else {
    let fact = 1;
    for (i = 1; i <= number; i++) {
        fact *= i;
    }
    console.log(`The factorial of ${number} is ${fact}.`);
}

//output
Enter a positive integer: 5
The factorial of 5 is 120.
Posted by: Guest on August-07-2021

Code answers related to "how to find factorial of a number in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language