 # freeCodeCamp Challenge Guide: Factorialize a Number

My solution

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

factorialize(10);``````
5 Likes

Here is my solution. It passed the tests, but wanted to see if this solution would cause issues. Right off the bat, i see that it cannot process Negative numbers.

I defined factor and set it equal to 1. if 0 or lower, my program spits out 1. it did pass the test though.

function factorialize(num) {
var factor = 1;
for (var x = 1; x <= num; x++) {
factor *= x;
}
return factor;
}

factorialize(5);

How could I change this to also include negative numbers?

2 Likes

One thing is you want your function to be reusable and to do the same thing every time it’s called. If you declare fact globally, you never ‘reset’ it to 1 when you run the function a second time or a third. Essentially, you’re changing the value of fact every time the factorialize(x) is run.

1 Like

Because then the value of your fact variable will be changed each time you run your factorialize() function, it won’t reset back to 1 when you’re done.

Example:
factorialize(3); //returns fact as 1321=6 since it’s the first time you used it.
factorialize(4);//returns fact as 6
432*1=144 instead of 24 because you changed your global variable fact’s value from 1 to 6 when you ran factorialize(3).

What’s wrong with this code, please?

function factorialize(num) {
var fact = 1;
for (var i = num; i <= num ;i–){
fact = fact * i ;
}
return fact;
}

This one is a basic for loop with a special condition if factorial is 0

``````

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

factorialize(5);
``````

This is one liner version answer using a ternary operator and recursion

``````function factorialize(num) {
return num===0 ? 1 : num*factorialize(num-1);
}

factorialize(5);
``````
9 Likes

you get infinite loop with your case because `i<=num` does not produce an end condition. The way you have it set up is such that “i” is always going to be less tha num

you should be using 0 as a stop condition, as factorials end when they hit a 0 value.

1 Like

This was my solution based on previous FreeCodeCamp examples:

``````var array = [];

function factorialize(num) {
for (i = 1; i <= num; i++) {
array = array.concat(i);
}

return array.reduce(function(previousVal, currentVal) {
return previousVal * currentVal;
}, 1);

}
factorialize(5);``````

This works for every number asked in the example, but Free Code Camp won’t accept this as an answer!? Why not? It works!

1 Like

When you forget about recursion & do it the long way…

function factorialize(num) {
var factorialArray = [];
factorialArray.push(num);
if (num > 1) {
while (num > 2) {
var value;
value = num - 1;
factorialArray.push(value);
num–;
}
return factorialArray.reduce(function(a, b) {
return a * b;
});
} else return 1;
}
factorialize(5);

1 Like

Worked for me! )

function factorialize(num) {
if (num >= 0) {
if (num % 1 == 0) {
if (num === 0 || num === 1) {
return 1;
} else {
return num * factorialize(num - 1);
}
}
return “must be whole number!”;
}
return “must be natural number!”;
}

factorialize(2.5);

Is this by any chance feasible by looping the numbers up to <= num and then multiplying each element of the resulting array by num and summing it all?
If yes: how? If no: how much am I wrong and why?

Here is a simple solution using a while loop and it works fine. No need to use additional if statement for returning 1 when num = 0:

function factorialize(num) {
var a = 1;
while (num > 0) {
a *= num;
num–;
}
return a;
}

factorialize(0);

4 Likes

here is my solution

``````function factorialize(num) {
var i;
var factor=1;
for (i=num;i>0;i--){

factor*=i;
}

return factor;
}

factorialize(5);
``````

solution provided here in first post suggest that function can call it self inside the function, am I right?

after some thought I realise that solution should be

``````function factorialize(num) {
if (num < 0) {retun "error";}
if (num === 0) { return 1; }
return num * factorialize(num-1);
}

factorialize(5);
``````

if input is negative value than it will result in “stack overflow”, isn’t it?

2 Likes

My code-

``````
function factorialize(num) {
var factorial=1;
if(num<=0){
return 1;
}
for(i=num; i>0; i--){
factorial = factorial*i;
}
return factorial;
}

factorialize(5);
``````

I finally got it after a week… 1 Like

Very clean and simple solution. I like it. Its easy to understanding what its doing. I am still learning JS and i wish more solutions could be tis simple and straightforward

1 Like

I know this ins’t the best answer but I used almost the same logic and it worked, check it out :

function factorialize(num) {

var arr = [];

if (num == 0) {

``````return 1;
``````

}

while ( num > 0) {

``````arr.push(num);
num--;
``````

}

var newArr = arr.reduce(function (x,y){
return x*y;

});

return newArr;

}

So why is this “factorialize(0) should return 1.”?

looking at example

Factorials are often represented with the shorthand notation n!
For example: 5! = 1 * 2 * 3 * 4 * 5 = 120

Doesn’t this mean 0! = 0 = 0?

3 Likes

After I failed with loops I figured I should turn the number to an array and multiply the numbers. Clearly it’s a more complicated version, which took me like 45 minutes to figure out.

But it works. ``````function factorialize(num) {

if (num === 0) {return 1;}
var numArray = Array.from({length: num}, (i, num) => num +1); //creates an Array from 1 to num

var multiplyed = numArray.reduce(function(a,b) {
return a * b;
}); // multiplies each number

return multiplyed;

}

factorialize(5);``````

I actually had the exact same approach (below) to create an array of all integers from num to 1, and then use reduce to multiply all those integers.

It’s not as elegant as the other correct answers, but it seems to work fine, yet isn’t accepted as an answer. Hmm…

``````var factArray = [];

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

for (myVar = num; myVar >= 1; myVar--) {factArray.push(myVar);}

num = factArray.reduce(function(a,b) {return a * b;});
return num;
}

factorialize(5);``````
2 Likes