# freeCodeCamp Algorithm Challenge Guide: Factorialize A Number

freeCodeCamp Algorithm Challenge Guide: Factorialize A Number
0

#1

Remember to use `Read-Search-Ask` if you get stuck. Try to pair program and write your own code

## Problem Explanation:

Return the factorial of the provided integer. If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.

Factorials are often represented with the shorthand notation n!

For example: `5! = 1 * 2 * 3 * 4 * 5 = 120`

## Hint: 1

This one starts easily since `0! = 1`, so you can go ahead and simply `return 1` there.

We can use that as an `if` in order to break the loop weāre going to create using a recursive function. It will check if the number you gave the function is 0 (which would be the end of your factorial chain). Functions āendā when they return anything. In fact, all functions without an explicit `return` statement will return `undefined`.

This is also why instead of having āfinishedā, a function is always said to āhave returnedā. And now thisā¦

try to solve the problem now

## Hint: 2

Understanding recursion

Recursion refers to a function repeating (calling) itself. In this case we are basically returning the given number (i.e. 5), multiplied by the function itself but this time the value passed to the num parameter is `num-1` (which initially translates to 4). The very function is going to run inside itself interesting, eh?

try to solve the problem now

## Hint: 3

Understanding the flow

The first returned value can be visualized better if you think about those parenthesis operations you did in secondary school where you do the math inside every parenthesis from inside out, bracket and square bracket until you get a final result (a total). This time itās the same thing, look at the program flow:

### During the first execution of the function:

[num = 5]

Is 5 equal to 1 or 0? No ā> Oki doki, letās continueā¦

Returns:

(5 _(second execution: 4 _(third execution: 3 _(fourth execution: 2 _fifth execution: 1))))

What it returns can be viewed as `(5*(4*(3*(2*1))))` or just `5 * 4 * 3 * 2 * 1`, and the function will return the result of that operation: `120`. Now, letās check what the rest of the executions do:

### During the rest of the executions:

Second Execution: num = 5-1 = 4 -> is num 0 or 1? No

ā> return the multiplication between 4 and the next result when num is now 4-1.

Third Execution: num = 4 - 1 = 3 -> is num 0 or 1? No

ā> return the multiplication between 3 and the next result when num is now 3-1.

Fourth Execution: num = 3-1 = 2 -> is num 0 or 1? No

ā> return the multiplication between 2 and the next result when num is now 2-1.

Fifth Execution: num = 2-1 = 1 -> is num 0 or 1? Yep

ā> return 1. And this is where the recursion stops because there are no more executions.

Got it?

try to solve the problem now

## Solution 1:

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

factorialize(5);
``````

## Code Explanation:

Notice at the first line we have the terminal condition, i.e a condition to check the end of the recursion. If `num == 0`, then we return 1, i.e. effectively ending the recursion and informing the stack to propagate this value to the upper levels. If we do not have this condition, the recursion would go on until the stack space gets consumed, thereby resulting in a Stack Overflow.

## Solution 2:

``````function factorialize(num) {
for (a = 1;num >= 1; num--) {
a = num * a;
}
return a;
}

factorialize(5);
``````

## Code Explanation:

We use a āFor Loopā to decrease the value of ānumā by 1 at each iteration and we stop the loop after ānumā reaches 1. It is important not to start with āaā assigned a value of 0 as the function will then keep returning 0.

## NOTES FOR CONTRIBUTIONS:

• DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
• Categorize the solution in one of the following categories ā Basic, Intermediate and Advanced.

See `Wiki Challenge Solution Template` for reference.

Discussion, Questions, and Resources for Part 2 (JavaScript Basic Algorithms, Object-Oriented, and Functional Programming - April 2018 Cohort)
#2

The solution code doesnāt appear to workā¦?
Maybe Iām missing something but I tried it as above and did not work.

#3

It works fine for me. Check this.

#4

#5

#6

who does the credit for the solution of this go too

#7

pls is factorialize a real function in javascript? or can a function of itself be declared inside it self, is dat possible

#8

Itās a function you define yourself. Checkout āRecursionā under the Relevant Links section above:

Recursion

#9

Just wondering the pros and cons of these two solutions and which if either is better and why.

I came up with this solution.

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

}
return factode;
}

factorialize(5);
``````

but the wiki provides this solutionā¦

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

factorialize(5);
``````

#10

Here are my two versions (iterative and recursive). The solution code at the top for this challenge does not cover the case where num is negative such as in factorialize(-2). My solutions return undefined for this edge case.

``````// iterative version
function factorialize(num) {
if (num < 0) return;
for (var product=1; num > 0; num--) product *= num;
return product;
}

// recursive version
function factorialize(num) {
return num < 0 ? undefined : num <= 1 ? 1 : num * factorialize(num - 1);
}
``````

#11

exactly i did it the way we were taught when we were kids in basic level languages so i did the first way u commented and it worked.

#12

This was my solution too! Clearly the wiki solution is more elegant, but I would also be interested in an explanation of the pros and cons.

#13

var fact=1;

function factorialize(num) {
for(var i=num;i>0;iā){
fact=fact*i;
}
return fact;
}

factorialize(10);
can u tell me why canāt I declare the var fact globallyā¦?

#14

var fact=1
function factorialize(num) {
for(var i=num;i>0;iā){
fact=fact*i;
}
return fact;
}

factorialize(10);

what is the problem in declaring var fact=1 globally instead of locally?

#15

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

#16

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?

#17

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.

#18

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).

#19

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

#20

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

Discussion, Questions, and Resources for Part 2 (JavaScript Basic Algorithms, Object-Oriented, and Functional Programming - April 2018 Cohort)