freeCodeCamp’s Basic Algorithm Scripting section – ‘Factorialize a Number‘
We’ll start digging into the freeCodeCamp (FCC) curriculum with ‘Factorialize a number’ because it’s pretty straight forward and it’ll allow us to discuss the structure of functions and introduce loops (This problem can also be solved with recursion, but we’ll get into that with some of the more complex algorithms).
The challenge is to write a function that will return the factorial of the number provided.
To understand factorials better, feel free to check out wikipedia, but basically, a factorial is the product of all integers less than or equal to n and is expressed as n! (i.e. 4! = 4 x 3 x 2 x 1 = 24).
FCC provides the following starting point:
1 2 3 4 5 |
function factorialize(num) { return num; } factorialize(5); |
As I mentioned in my first post, a function is a block of code that acts independently to perform a task. It can be called from other parts of your program, and therefore, can be used over and over to perform the same ‘function’. Functions have many uses in JavaScript, check out Eloquent JavaScript – chapter 3 to learn more.
As you can see above, the function we’ll be building is named factorialize and has one parameter (num). The way it’s written above, it returns that parameter (the body of a function is contained in {}). The function is then called on line 5 and passed the argument (5).
We need to add the code that will take the parameter (num) and calculate and return the factorial of ‘num’.
We’ll start by declaring a variable to store the value of the factorial and set it equal to 1.
1 |
var total = 1; |
We’ll add the code to calculate the factorial using a while loop. Loops allow us to set a condition, then execute a block of code until that condition is met. MDN gives a pretty good intro to while loops here.
We’ll use the argument (num) in our condition and say:
1 2 |
while(num>1){ } |
This while loop will now iterate until num>1 is false. Therefore, we have to remember to somehow change num each time so that eventually the loop will end (otherwise, we have an infinite loop and the program will never complete).
Now we have to do something each time the loop iterates, this is where our variable total comes in. Given that JavaScript will execute the right side of an equation before it stores it to the left side, we can set total = total * num, then depricate num by 1. That would look something like this:
1 2 3 4 |
while (num > 1) { total = total * num; num = num -1; } |
If we walk through this for num = 5, the first pass would be
1 2 |
total = 1 * 5; num = 5 -1; |
Now total = 5 and num = 4. Since 4 > 1, the while loop will execute again.
1 2 |
total = 5 * 4; num = 4 - 1; |
Now total = 20 and num = 3. This will continue until the while (num>1) fails. It would look like this.
1 2 |
total = 20 * 3; num = 3 - 1; |
total = 60, num = 2.
1 2 |
total = 60 * 2; num = 2 - 1; |
total = 120, num = 1.
At this point, the while loop condition would be false, and the loop would end with total = 120. Given 5! = 120, we have the right total, we just need to return it. We can just change the return statement from return num; to return total;.
Putting everything together:
1 2 3 4 5 6 7 8 |
function factorialize(num) { var total = 1; while (num > 1) { total = total * num; num = num -1; } return total; } |
This gives us the correct answer, but JavaScript makes statements that add, subtract, multiply or divide a number by itself much easier to write using the following operators: +=, -=, *= and /=. You can also increment and deprecate by 1 using ++ and — respectively. See the w3schools page on operators for more info. Knowing that, the solution seen above can be written:
1 2 3 4 5 6 7 8 |
function factorialize(num) { var total = 1; while (num > 1) { total *= num; num--; } return total; } |
These two solutions do the exact same thing and solve the ‘Factorialize a Number’ algorithm challenge.
Hopefully this straightened out the code for you. See you next time.
-Jeremy