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?

freeCodeCamp Challenge Guide: Factorialize a Number

0

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

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;});

}