Three Ways to Factorialize a Number in JavaScript
Sonya Moisset
1.3K28

I used for loop & reduce to achieve the solution.

function factorialize(num) {
 var arr = [];
 for (var i=1; i<=num; i++){
 arr.push(i);
 }
 //console.log(arr);
 num = arr.reduce(function(a,b){
 return a*b;
 }, 1);
 return num;
}
factorialize(20);

At first, I’d defined the array in the global space and for that reason editor was printing out the right result but it wasn’t taking the answer as correct. So, I moved var arr = []; within the function. And rightly so, because one should avoid using a global variable as much as possible.