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
Relevant Links
Spoiler Alert!
Solution ahead!
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.
Relevant Links
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.
- Add an explanation of your solution.
- Categorize the solution in one of the following categories ā Basic, Intermediate and Advanced.
- Please add your username only if you have added any relevant main contents. ( DO NOT remove any existing usernames)
See
Wiki Challenge Solution Template
for reference.