Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript factorial

function factorial(n) {
  if (n < 0) return;
  if (n < 2) return 1;
  return n * factorial(n - 1);
}
Comment

Factorial function javascript

function getFact(number) {
    let fact = 1;
    for (var i = 1; i <= number; i++) {
        fact = fact * i;
    }
    return fact;
}
console.log(getFact(5))
//Output: 120
Comment

factorial function javascript

    function factorial(n) {
        if (n > 1) {
            return n * factorial(n - 1);
        }
        return 1;
    }
Comment

factorial of number js

const factorialize = num => {
  if (num === 0 || num === 1) {
    return 1;
  }
  for (let i = num - 1; i >= 1; i--) {
    num *= i;
  }
  return num;
};
Comment

Program for factorial of a number in javascript

<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>
Comment

Factorial in javascript

function factorial(n) {
    if (n == 1) {
        return 1;
    }
    return n * factorial(n - 1);
}
console.log(factorial(6));
//Output: 720
Comment

factorial javascript function

function factorialize(num) {
  if(num < 2) return 1;
  return num *= factorialize(num - 1);
}
Comment

how to return a factorial in javascript

function factorial(n) {
    if (n == 1) {
        return 1;
    }
    return n * factorial(n - 1);
}
console.log(factorial(6));
Comment

javascript factorial of a number

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

// Examples
factorial(2);   // 2
factorial(3);   // 6
Comment

js how to calculate factorial

// @ts-check
/* ---------------------------------- using a loop --------------------------------- */
(() => {
  function factorial(n) {
    if (n < 0) return;

    let result = 1;

    for (let i = 2; i <= n; i++) {
      result *= i;
    }

    return result;
  }

  console.log(factorial(4)); // => 24
  console.log(factorial(0)); // => 1
  console.log(factorial(-2)); // => undefined
})();

/* ------------------------------ recursive way----------------------------- */
(() => {
  function factorial(n) {
    if (n < 0) return;
    if (n == 0) return 1;

    return n == 1 ? 1 : n * factorial(n - 1);
  }

  console.log(factorial(5)); // => 120
  console.log(factorial(0)); // => 1
})();
Comment

JavaScript Find Factorial

// program to find the factorial of a number
function factorial(x) {

    // if number is 0
    if (x === 0) {
        return 1;
    }

    // if number is positive
    else {
        return x * factorial(x - 1);
    }
}
const num = 3;
// calling factorial() if num is non-negative
if (num > 0) {
    let result = factorial(num);
    console.log(`The factorial of ${num} is ${result}`);
}
Comment

factorial program in javascript

function fact(N){
  if(N <= 1){
  	return "1";
  }
  else
  {
    return N * fact(N-1);  
  }
}

fact(5);
Comment

Factorial Function Javascript

var factorial = 1;
function myfact(fact) {
    for (let i = 1; i <= 5; i++) {
        factorial = factorial * i;
    }
    return factorial;
}
console.log(myfact(factorial))
 // Output: 120
Comment

how to write a factorial function in javascript

function factorial(num){
	return num === 1 ? 1 : num * factorial(num - 1) 
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: read file with deno 
Javascript :: ejs with express 
Javascript :: Copy document DOM without reference 
Javascript :: js split text on spaces 
Javascript :: nesting for loops 
Javascript :: js dom get website name 
Javascript :: how to use rgba in react native 
Javascript :: how to add comment in react js 
Javascript :: how to disable right click in javascript 
Javascript :: export type you may need an appropriate loader to handle this file type 
Javascript :: jquery post upload file 
Javascript :: event listener to elements with class 
Javascript :: autherization token in axios 
Javascript :: react router dom v6 goback 
Javascript :: Unhandled rejection TypeError: Article.findById is not a function sequelize 
Javascript :: send a message when a bot joins your server discord.js 
Javascript :: how to view local storage in chrome 
Javascript :: dont drag img 
Javascript :: javascript get date of last monday 
Javascript :: how to get current year in nodejs 
Javascript :: string to number js 
Javascript :: javascript to mask email address 
Javascript :: how to execute javascript after c# function execute 
Javascript :: js document ready 
Javascript :: justifycontent react native flatlist 
Javascript :: $.ajax async false 
Javascript :: react-native loading spinner 
Javascript :: javascript determine array type 
Javascript :: js download json 
Javascript :: error: Error: Unable to resolve module `react-native-gesture-handler` from `node_modules@react-navigation ativelibmoduleScrollables.js`: react-native-gesture-handler could not be found within the project. 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =