Let’s say you originally called this function as follows:

multiply([2,3,4], 3)

The first time through, since n > 0, you would hit the else block and thus the original function call would return:

multiply([2,3,4], 2) * 4

The original function call is returning the number 4 (the value at arr[n-1] in which n=3 for the original call) multiplied by the value returned by the function called again but this time with n set to one less than the original call.

So now the recursive second function call [multiply([2,3,4], 2)] is evaluated and it also hits the else block so it returns:

multiply([2,3,4], 1) * 3

So now we can go back to the original function call return value, which as a reminder was:

multiply([2,3,4], 2) * 4

and replace the call to multiply([2,3,4], 2) with the value it returns:

(multiply([2,3,4], 1) * 3) * 4

Note: I added the unnecessary parens just to make it clear what we just added.

What we have now is still the original return value but just one level deep into the recursion. Do you see how each time we recursively call multiply() we are adding another number to be multiplied in the original function call return value?

calls this function : multiply([2,3,4], 3)
multiply(arr, n-1) * arr[n-1];
multiply ([2,3,4], 2) * arr[2];
multiply([2,3,4], 2) * 4;
and the answer is 24 (says the console), but how??

Why is multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1] ?
like, why n-1? and not just n?

I ve been googling up for 1+hr and looking through diff videos, and I am still confused. I feel like my brain is just stuck…in some logic thinking…

Hey xxwydxx, so hopefully I can explain part of this.

On the first recursion you get

multiply([2,3,4], 2) x 4
On the second recursion you get

multiply ([2,3,4], 1) x 3
On the last recursion you get

multiply([2,3,4], 0) x 2 —> This is where the recursion stops since n<=0.

Now, you have three value that have been returned to the computer 2 and 3 and 4. Since the recursion is finished the computer can now multiply the numbers. This is the call stack. The call stack can be collapsed/completed only after the function is finished. Since the function calling has completed the computer now counts backwards from this itemized call stack and multiplies each time.

So, the last number being 2 is then multiplied by 3 and then by 4:

Answer returned is 2 x 3 x 4 = 24.

Hopefully that wasn’t too confusing. Maybe try researching the call stack in recursive functions. That helped me a lot.

As for the second part of your question:

This is the last part that I have not understood either. Maybe someone else can chime in on this one.

If you go over to repl.it, you can create a new repl. When you copy the link to the repl and paste it into a forum post, the repl will embed like above.