Basic JavaScript - Replace Loops using Recursion

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

Link to the challenge:

In mathematics, the idea of breaking up a number or a problem in smaller parts is common. So try to consider this like a maths problems.

say we have a list of numbers 1, 2, 3, 4, 5
The return is saying, the product of these numbers is equal to
the product of 1, 2, 3, 4 times 5

And the product of 1, 2, 3, 4 in turn is also returning
the product of 1, 2, 3 times 4

And the product of 1, 2, 3 is also returning
the product 1, 2 times 3

etc.
The problem gets smaller each time we recurse (each time multiple calls itself)

1 Like

I found it useful to understand recursive functions as unravelling from the inside out.
Here is an article which explains it very well I think.

As for this current challenge, here are the notes which I took, explaining the unravelling for example values:

multiply([1, 2, 3, 4, 5, 6], 4);

This works in practice as follows:

  1. Base case (n<=0) fails
  2. return multiply(arr, 3)*arr[3] ) // multiply(arr, 3) * 4
  3. Base fails
  4. return multiply(arr, 2)*arr[2]) // multiply(arr, 2) * 3
  5. Base fails
  6. return multiply(arr, 1)*arr[1]) // multiply(arr, 1) * 2
  7. Base fails
  8. return multiply(arr, 0)*arr[0]) // multiply(arr, 0) * 1
  9. Base is reached and function stops // returns 1

Function returns values from the inside out:
1 * 1 * 2 * 3 * 4 = 24.

1 Like

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

1 Like

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.

1 Like

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.

2 Likes

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.

Thanks man, will definitely give this one a check.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.