**The Challenge:**

1 2 3 4 5 6 7 |
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 |

**The solution should return:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
factorialize(5) should return a number. factorialize(5) should return 120. factorialize(10) should return 3628800. factorialize(20) should return 2432902008176640000. factorialize(0) should return 1. |

**Some helpful links:**

**What my solution should achieve:**

- Should return the factorial of the provided integer.
- Should take into consideration that if the integer is represented with the letter n, a factorial is the product of all positive integers <= n.
- Factorials are often represented with the shorthand notation n!
- For example, 5! = 1 * 2 * 3 * 4 * 5 = 120

**Starter Code:**

1 2 3 4 5 |
function factorialize(num) { return num; } factorialize(5); |

I decided to go the recursive route with this algorithm because I really want to nail down this very important programming technique. I could have gone the iterative route with a while loop, but been there done that with while loops. Haven’t done too many recursive functions, however.

So what is recursion? Recursion is a programming technique whereby a function calls itself. I use recursion in the Smallest Common Multiple algorithm, and I wrapped it in a for loop. Someone saw it yesterday, and asked me why did I do that? Wasn’t I going to potentially increase the call stack to such a degree that it would return the error “RangeError: **Maximum call stack** size **exceeded**“. That means that somewhere in a piece of code, a function is being called which in turn calls another function and so on, until it reaches the call stack limit. This is almost always because of a recursive function with a** base case** that is ** not** being met. I liked what it did, and I wanted to learn something new. I guess this will give me plenty to talk about when I delve more into Recursion and its potential pitfalls!

I have not been focusing so much on call stacks, scalability, time, and memory so much to date. However, when I have finished my written articulations of algorithms I have completed to date on Free Code Camp, which is pretty much all of them except a few advanced ones which I will get back to after a small break, I will start focusing on ** Big O**,

**and its potential pitfalls, more on**

*Recursion***,**

*Cyclomatic Complexity***,**

*Dynamic Programming***, and so on.**

*Unit Testing*First things first. Written articulation of the code!

I knew that my integers needed to be positive. That meant they needed to be > 0. I also knew that I had to take into account the range from num down to 1. Let’s analyze the code and see how it works:

Since I needed my integers to be positive and therefore > 0, I had to make sure that anything < 0 was rejected. Therefore:

1 2 3 |
if (num < 0) { return -1; } |

If you recall from some of my prior articulations, -1 usually represents the lack of existence of something or in this case, the rejection of something. Here we are rejecting any integer below 0.

Next I had to take into consideration the possibility of 0:

1 2 3 |
} else if (num === 0) { return 1; } |

So if num is === 0, factorial is 1. Therefore, return 1.

* An aside:* But why is 0! (factorial 0) = 1? I didn’t know why, and it just didn’t seem intuitive. I conducted a Google Search and came up with a series of videos on Khan Academy on Permutations and factorial and came up with the answer.

According to Khan Academy, factorial appears the most in permutations and combinations. Purportedly 0! = 1 is a very useful concept. It is also an arbitrary one. Looking at 0! for the first time, you might think that 0! would be 0. But as we already know from the solution here, for instance, this is NOT the case. You can’t prove 0! = 1. You can only “define” it.In other words, it was “invented” and is a useful notation. To learn more, you can listen to Khan Academy’s video on 0! = 1 or read an interesting thread on math.stackexchange.com on the subject.

Next we have:

1 2 3 |
} else { return (num * factorialize(num - 1)); } |

And this is where our lovely ** recursive function** resides. Since else refers to everything else, we will start with the provided num factorial, 5 and do a bit of substitution:

return (5 * factorialize(5 – 1) * (4 – 1) * (3 – 1) * (2 – 1)) – > 120.

**Same as **

5 * 4 * 3 * 2 * 1 -> 120.

So instead of using a while loop, for example, to calculate the value of factorialize(5), we call it gain and again in the return statement as shown above until the value of n – 1 = 1. Then the recursion stops.

Lets take a look at factorialize(10) focusing only on the last return statement:

return (10*factorialize(10 – 1)*(9 – 1)*(8 – 1)*(7 – 1)*(6 – 1)*(5 – 1)*(4 – 1)*(3 – 1)*(2 – 1) -> 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 -> 3628800

And so on.

**Note:** According to the arguments and expected outcomes I was provided for this algorithm, I could have removed if (num < 0), because there was no associated argument and expected outcome for it. However, to include it means taking more possible arguments and outcomes into consideration.

**JSHint Metrics:**

1 2 3 4 |
There is only one function in this file. It takes one argument. This function contains 4 statements. Cyclomatic complexity number for this function is 3. |

**Solution:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function factorialize(num) { "use strict"; // if num < 0, reject it if (num < 0) { return -1; // num === 0 its factorial is 1 } else if (num === 0) { return 1; } else { return (num * factorialize(num - 1)); } } factorialize(5); |

**Solution in ES6:**

1 2 3 4 5 6 7 8 9 10 11 12 13 |
function factorialize(num) { // if num < 0, reject it if (num < 0) { return -1; // num === 0 its factorial is 1 } else if (num === 0) { return 1; } else { return (num * factorialize(num - 1)); } } factorialize(5); |

This was first published here on **Jul 12, 2016 @ 01:19**. It was part of the freecodecamp.org’s **Basic Javascript Algorithm Scripting** for the **Front End Development**certification.

Comment Policy:Your words are your own, so be nice and helpful if you can. Please, only use your real name and limit the amount of links submitted in your comment. We accept clean XHTML in comments, but don't overdo it please. Comments are moderated, so spammy or malicious links will be removed.