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:

Factorialize a Number Starter Code

JavaScript

1

2

3

4

5

functionfactorialize(num){

returnnum;

}

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, Recursion and its potential pitfalls, more on Cyclomatic Complexity, Dynamic Programming, Unit Testing, and so on.

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:

JavaScript

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:

Factorialize a Number Solution

JavaScript

1

2

3

}elseif(num===0){

return1;

}

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:

Factorialize a Number Solution

JavaScript

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:

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:

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.

Share this:

Like this:

Related

Maria Campbell

Founder, Inter-Global Media Network, Inc., where I focus on Front End Development. as well as a digital photographer, videographer, blogger, broadcaster, and graphic designer. Genesis Framework user. I got my start 8+ years ago teaching myself HTML/CSS and Wordpress Development because I couldn't find anyone who could create what I wanted. I have been working with open source software ever since. I am an open source software evangelist, and a "Women in Technology" evangelist. A creative professional myself, I find Web Development to be both practical and creative. I love to cook, travel, read mysteries, love everything French, the beach at sunrise, my three cats, and hope to revisit the city of San Francisco one day.

Reader Interactions

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.