# freeCodeCamp Challenge Guide: Factorialize a Number

``````function factorialize(num) {
newNum = 1;
for(var i = 1; i<=num; i++){
newNum *= i;
}
num = newNum;
return num;
}

factorialize(0);

``````

Not fancy, but it works. Recursion should be it’s own lesson!

//declare an array
var arr= [];
function factorialize(num) {
//check if num is 0
if(num==0){
return 1;
}
else{
for(var i=1; i<=num; i++){
arr[i]= i ; // arr=[1,2,3,4,5]
}
}
//use reduce method to multiply values of array and reduce to one value
num=arr.reduce(function(a,b){
return a*b;
});
return num;
}
factorialize(0);

Here is 3 type of solution:

1. With “if”

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

2. With “While loop”

function loopFactorial(num) {
var result = num;
while (num > 1) {
result = result * (num-1);
num–;
}
return result;
}

3. With “ForLoop”

function forLoopFactorial(num) {
var result=1;
for (var i = num; i>0; i–){
result *= i;
}
return result;
}

2 Likes

This answer is really elegant, thanks!

1 Like

Took alot more than the spoiler. None the less, working. Happy Monday.

function factorialize(num) {
var total = [],
newNum,
sum = 1;
while(num > 0) {
newNum = num;
total.push(newNum);
num–;
}
for(var i = 0; i < total.length; i++) {
sum = sum * total[i];
}
return sum;
}

factorialize(10);

Came up with this one - does the job

function factorialize(num) {
for (var i = 0; i <= num; i++) {
var calculate;
if (num === 0) {
calculate = 1;
} else if (i === 1) {
calculate = i * i;
} else {
calculate *= i;
}
}
return calculate;
}
factorialize(0);

That’s what I was trying to do also and it makes more sense to me as a beginner why this works versus the wiki provided one. I would also like to know why the wiki solution is “better”

my solution:

``````function factorialize(num) {
var fnum = 1;
for (var i = 1; i <= num; i++){
fnum *= i;
}
return fnum;
}
factorialize(0);``````

It took me a few tries and tweaks to get it right. I think what is “better” about the wiki solution is probably that it is more efficient, not just shorter. Where ‘if’ statements have to make one or more comparisons and ‘for’ loops have to iterate through ALL numbers less than (or greater, depending on which direction you run the loop) the provided integer, the recursive function is only performing a mathematical operation, after the initial ‘if’ statement which will only run one time. This may not seem like a huge difference when dealing with numbers between 0 and 20, like the tests in the exercise, but if you were to factorialize a number like 10,000 or 1,000,000+ the run time would greatly increase.

1 Like

Does anybody know why this isn’t accepted? it works.

my code -
var storage = 1;
function factorialize(num) {
for (var i = num; i>1 ; i–){
num = storage *= i;

}
return num;
}

factorialize(10);

I feel dumb, I did it different:

function factorialize(n) {
var f = [1];
for (var a = 1; a <= n; a++) {
f.push(a);
}
return f.reduce(function(a,b){return a*b;});
}

2 Likes