Basic JavaScript - Replace Loops using Recursion

Tell us what’s happening:

I don’t understand what is happening in the example function with recursion. Specifically this line:

return multiply(arr, n - 1) * arr[n - 1];

My question is: **How can you multiply the function multiply(arr,n-1) to an individual index within the array? I just don’t understand what this is doing. **

  • I do understand broadly that adding -1 to n is our means of decrementing in the recursive function, but I don’t understand how you can multiply arr[n-1] to multiply(arr, n - 1).

(Yes, I have searched through the existing topics on this forum, but the responses either go on a tangent or explain things in a way I don’t understand).

  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Replace Loops using Recursion

Link to the challenge:

You aren’t multiplying by an index. You are multiplying by an individual element from the array.

Look at a specific example.

const arr = [3, 5, 8];

That line is saying that multiply(arr, 3) === multiply(arr, 2) * arr[2], or 'the product of all three numbers is the same thing as the product of the first two numbers multiplied by the third number`.

Hi JeremyLT, thanks for the response.

So, i’m more clear on why multiply(arr, 3) === multiply(arr, 2) * arr[2] is true, but I’m still not understanding how the function knows that n corresponds to the number of elements in the array (e.g., multiply(arr,3) --> multiply([3,5,8], 3) == 3*5*8).

In other words, how does it not that n corresponds to the nth number in the array?

n is the nth value in the array though.

The call multiply([3,5,8], 3) will result in a recursive call multiply([3,5,8], 2)

Ok, I think I understand what my issue was. I wasn’t totally understanding what was happening in the call stack. Correct me if I’m wrong, but this is what I’m understanding now:

  1. Starting with n=3, multiply has to return the second return statement (return multiply(arr, n - 1) * arr[n - 1];) because n=3 > n=0.

  2. The actual value of multiply with n=3 gets placed at the bottom of the call stack since the return statement includes the function (multiply) itself, and has nothing yet to return.

  3. Because we have n-1 in the function (multiply(arr, n-1)), n gets decremented to n=2.

  4. Steps 3-4 above get repeated, continually adding the function multiply with the different n values to the call stack.

  5. Once n = 0, multiply(arr, n-1) now has a return statement with a real value (1), so, the last call for the function (where n=0), gets removed from the top of the call stack.

  6. Now, we take the return value of the multiply function (i.e., multiply(arr, n-1) = 1), and we multiply that by the last index position referenced in the call stack: 1 * arr[1-1] == 1* arr[0] == 1*3. Now, the function multiply returns 3.

  7. That gets repeated over and over until you hit the last function call in the call stack, where n=3. At this point we get the return value of multiply (which returns 15) * arr[3-1] == 15*8. So, 15 * 8 = 120.

Hoping my logic makes sense. I’m still new to understanding how this works, but this logic got me past the recursion problem with the addition challenge. So, it’s worked at least in these two instances.

You’ve got the idea of how it works.

1 Like