Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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);
 
PREVIOUS NEXT
Tagged: #Factorialize #Number
ADD COMMENT
Topic
Name
9+6 =