# Basic JavaScript: Recursion problem

Tell us what’s happening:

I understand everything in this post, link at the bottom, except for maybe the most important part.

In this recursion example code block:

`````` function multiply(arr, n) {
if (n <= 0) {
return 1;
} else {
return multiply(arr, n - 1) * arr[n - 1];
}
}
``````

I do not understand multiply(arr, n-1) * arr[n-1];

Can someone please unpack this for me? I hope this is clear enough. I am new to coding. If it is not clear, please ask me, and I will update.

``````
function sum(arr, n) {
// Only change code below this line

// Only change code above this line
}

``````

User Agent is: `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36`.

Challenge: Replace Loops using Recursion

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?

6 Likes

Wow, amazing. Thank you. Dear bbsmooth,
I am still a bit confused.

1. how is the math broken down?

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

1. 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…

Thank you sooo much.

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

On the first recursion you get

1. multiply([2,3,4], 2) x 4
On the second recursion you get
2. multiply ([2,3,4], 1) x 3
On the last recursion you get
3. 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.

Best,
Paul

1 Like

Dear Paul
So …

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

multiply([2,3,4], 2) <-- this part isn’t included in the actual mathematical calculation then? even tho there is a multiplication sign behind it.

Since we are only multiplying all the arr[n - 1] that we got in the overall recursive function right?

Thank you ><

Does this interactive code help?

I am a big fan of always adding more `console.log`s to see what’s going on!

You can change the array and the size of the array to explore what the code is doing.

1 Like

Exactly, you got it. =)

how did you do this embed?

sorry just curious

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.

It’s one of my favorite features of repl.

2 Likes