The algorithm discussed in the post returns the factorial of an integer.

If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.

Factorials are often represented with the shorthand notation n!

For example: 5! = 1 * 2 * 3 * 4 * 5 = 120

function factorialize(num) {

//check if the number entered is equal to 0 or 1 if yes return 1 because 0 factorial is 1
if(num === 0 || num === 1){
return 1;
}

//initialize a result variable and set the value to 1
var result = 1;

//loop through the number, in each iteration of the loop, multiply the current
//value with the value stored in result and add it to result again

for(num; num > 0; num–){
result = result * num;
}

//return result
return result;
}

factorialize(10) should return 3628800.

factorialize(5) should return 120.

Factorialize a Number

Leave a Reply

Your email address will not be published. Required fields are marked *