I feel like the explanation of this is brutal or I’m just dumb. So I am running into a major block just trying to comprehend this explanation.
In the paragraph below is describing basically how multiply(arr, n) is equals to multiply(arr, n - 1) * arr[n - 1] but my question is . . .
How do i even calculate multiply(arr, n - 1) * arr[n - 1] to confirm that is even is REALLY equal to multiply(arr, n). Better yet, how do i even start multiplying an array?
The explanation is FreeCodeCamp:
However, notice that multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]. That means you can rewrite multiply in terms of itself and never need to use a loop.
function sum(arr, n) {
// Only change code below this line
function multiply(arr, n) {
if (n <= 0) {
return 1;
} else {
return multiply(arr, n - 1) * arr[n - 1];
}
}
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:108.0) Gecko/20100101 Firefox/108.0
Challenge: Basic JavaScript - Replace Loops using Recursion
Been reading up on this like crazy but i suppose this would mean that it would be a more elaborate for loop. Thanks for taking your time to participating @hbar1st
Thank you so much! I read the article, I have learned a great deal from it, without a day. I am glad to have a new site to help me break down various challenges/ concepts.
I will check the article out too, in which probably has my answer stored, but could I get a real life example of why or just where this would be used in development? I am very curious, and love math, I just haven’t been able to see how any of the complete off the wall mathematics ties into developing applications or any software other than working @system design. In which case, I would continue to shy away from for the time being as it all takes a lot of time and effort - I just try to stay as efficient as possible.
Probably the most quintessential example is traversing a tree structure (such as directories on a file system, or nodes on the DOM).
You can check out the pseudo-code implementations on Wikipedia and notice that the recursive examples are generally simpler (fewer lines of code, fewer conditional branches) than the iterative ones.
Yeah the link @igorgetmeabrain totally broke the process and meticulous parts of the code down which was somewhat digestible even for a total newbie like myself. I totally would recommend this to anyone trying to learn recursion. The finer parts are still a little bit of a mystery that i think i’ll spend two days breaking down.