Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Factorialize a Number

// Factorialize a Number

function factorialize(num) {
	if (num === 0) return 1;
	return num * factorialize(num - 1);
}

factorialize(5);

// OR with Tail Call Optimization (https://stackoverflow.com/questions/33923/what-is-tail-recursion)

function factorialize(num, factorial = 1) {
	if (num == 0) {
		return factorial;
	} else {
		return factorialize(num - 1, factorial * num);
	}
}

factorialize(5);
Comment

Factorialize a Number

function factorialize(num) {
  for (var product = 1; num > 0; num--) {
    product *= num;
  }
  return product;
}

factorialize(5);
Comment

find a factorial of a number

<?php
              
      $num = 3;
      $factorial = 1;
      if($num != 0) {
        for($i = $num; $i>=1; $i--) {
          $factorial = $factorial * $i;
        }
      }
      else {
        $factorial = 1;
      }
      echo "Factorial of $num is $factorial";

    ?>
Comment

factorize a number

function factorialize(num) {
  let product = 1;
  for (let i = 2; i <= num; i++) {
    product *= i;
  }
  return product;
}

factorialize(5);
Comment

factorize a number

function factorialize(num) {
  if (num === 0) {
    return 1;
  }
  return num * factorialize(num - 1);
}

factorialize(5);
Comment

factorize a number

function factorialize(num, factorial = 1) {
  if (num === 0) {
    return factorial;
  } else {
    return factorialize(num - 1, factorial * num);
  }
}

factorialize(5);
Comment

factorize a number

function factorialize(num) {
  return num < 0 ? 1 :
    new Array(num)
      .fill(undefined)
      .reduce((product, _, index) => product * (index + 1), 1);
}
factorialize(5);
Comment

PREVIOUS NEXT
Code Example
Javascript :: react build blank page 
Javascript :: lifecycles if reactjs 
Javascript :: fetch json data into array 
Javascript :: javascript convert input to lowercase 
Javascript :: vuejs transform observer to object 
Javascript :: regx to accept name 
Javascript :: foreach in react 
Javascript :: serializeobject jquery 
Javascript :: react load script after render 
Javascript :: how to create a website with javascript 
Javascript :: react time input 
Javascript :: what is super(props) in react 
Javascript :: Minimal Project Angular 
Javascript :: ajax is not a function 
Javascript :: hasownproperty javascript 
Javascript :: how to get class name of element in javascript 
Javascript :: react js form radio input using hooks 
Javascript :: javascript date set hours minutes seconds to 0 
Javascript :: js substr 
Javascript :: javascript check for duplicates in array 
Javascript :: get date from datepicker 
Javascript :: how to get circle around text in react natvie 
Javascript :: SyntaxError: await is only valid in async function 
Javascript :: how to add all values of array together js 
Javascript :: change terminal shortcut vscode 
Javascript :: p5js left mouse click 
Javascript :: play notification sound on chat js 
Javascript :: react 18 rendering twice 
Javascript :: javascript caeser cipher 
Javascript :: javascript typeof 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =