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
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/126.96.36.199 Safari/537.36
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, 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 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.
multiply([3,5,8], 3) will result in a recursive call
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:
multiply has to return the second return statement (
return multiply(arr, n - 1) * arr[n - 1];) because
The actual value of
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.
Because we have
n-1 in the function (
n gets decremented to
Steps 3-4 above get repeated, continually adding the function
multiply with the different
n values to the call stack.
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.
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 ==
1*3. Now, the function
multiply returns 3.
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.