# freeCodeCamp Challenge Guide: Factorialize a Number

freeCodeCamp Challenge Guide: Factorialize a Number
0

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?

2 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

Hello campers.I am figuring on how to paste my code here.Someone with an adea?highly appreciated.Thanks.

function factorialize(num) {

var factorial = num;

if(num === 0) {

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

}
return num * factorialize(num - 1);
}

factorialize(5);

This solution also checks for 0:

function factorialize(num) {

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

factorialize(0);

Hello, my function works (although it is more complicated than the posted answer), but it is not letting me advance to the next challenge.

Instead of just putting the answer and moving on, I want to understand why my code is not getting a “pass”.

Here is my code:

var array=[];

function factorialize(num) {

for (i=1; i<=num; i++){
array.push(i);
}
if (num===0){
return 1;
}
num = array.reduce(function(a, b) {
return a*b;
});
return num;
}

factorialize(5); //returns 120

Does anyone have insight on this?

Hope this help

function factorialize(num) {
var product =1;

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

for (var i=1;i<num; i++) {

``````product = product * i;
``````

}
return num* product;

}

factorialize(5);

Hi campers,I was teaching myself to post code here using back ticks.

``````
function factorialize(num) {

var factorial = num;

if(num === 0) {

return 1;
}
return num * factorialize(num - 1);
}

factorialize(5);``````
``````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;
}
//add values to array
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