fix(guide): add intermediate solution to guide (#25485)
This commit is contained in:
@ -82,7 +82,7 @@ Got it?  Code Solution:
|
||||
##  Basic Code Solution:
|
||||
|
||||
function factorialize(num) {
|
||||
if (num === 0) { return 1; }
|
||||
@ -103,6 +103,34 @@ Notice at the first line we have the terminal condition, i.e a condition to chec
|
||||
* <a href='https://en.wikipedia.org/wiki/Factorial' target='_blank' rel='nofollow'>Factorialization</a>
|
||||
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators' target='_blank' rel='nofollow'>Arithmetic Operators</a>
|
||||
|
||||
##  Intermediate Code Solution:
|
||||
|
||||
function factorialize(num, factorial = 1) {
|
||||
if (num == 0) {
|
||||
return factorial;
|
||||
} else {
|
||||
return factorialize(num - 1, factorial * num);
|
||||
}
|
||||
}
|
||||
|
||||
factorialize(5);
|
||||
|
||||
 <a href='https://repl.it/repls/CrimsonVerifiableDownload' target='_blank' rel='nofollow'>Run Code</a>
|
||||
|
||||
## Code Explanation:
|
||||
|
||||
In this solution, we use <a href='https://stackoverflow.com/questions/33923/what-is-tail-recursion' target='_blank' rel='nofollow'>Tail Recursion</a> to optimize the the memory use.
|
||||
|
||||
In traditional head recursion, the typical model is that you perform your recursive calls first, and then you take the return value of the recursive call and calculate the result. In this manner, you don't get the result of your calculation until you have returned from every recursive call.
|
||||
|
||||
In tail recursion, you perform your calculations first, and then you execute the recursive call, passing the results of your current step to the next recursive step. This results in the last statement being in the form of (return (recursive-function params)).
|
||||
|
||||
In this solution, with each evaluation of the recursive call, the factorial is updated. This is different from the head-recursive solution where all evaluation calculations are stored on the stack until the base case is reached.
|
||||
|
||||
### Relevant Links
|
||||
|
||||
* <a href='https://www.geeksforgeeks.org/tail-recursion/' target='_blank' rel='nofollow'>Tail Recursion</a>
|
||||
|
||||
##  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.
|
||||
|
Reference in New Issue
Block a user