# Basic JavaScript: Replace Loops using Recursion - Passing but not getting it

Tell us what’s happening:
Hi all,

I got to pass the challenge just by following the same logic as in the example provided but I didn’t feel like moving on since I wasn’t able to understand what I had done. So I check the blog post in the Watch a Video section. The concept there became clear (-er) but when I look back at the example and my solution in the exercise, I still get stocked at this line:

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

How does the function know what to sum (or multiply) to `arr[n]`? The first term `(arr, n - 1)` is an array of numbers and a decreasing number, so where in the function are we instructing to take 1 number from the array and sum it to `arr[n]`. Are we then pushing this to `arr[0]`? Is there more in this function that is not visible the learner?

Thanks in advance to anyone who can dumb-down this whole thing.

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

// Only change code above this line
}

``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:70.0) Gecko/20100101 Firefox/70.0`.

Challenge: Replace Loops using Recursion

1 Like

Blockquote How does the function know what to sum (or multiply) to `arr[n]` ? The first term `(arr, n - 1)` is an array of numbers and a decreasing number, so where in the function are we instructing to take 1 number from the array and sum it to `arr[n]` . Are we then pushing this to `arr[0]` ? Is there more in this function that is not visible the learner?

The function doesn’t know; it calls itself again with the multiply(arr, n-1) or in this case sum(arr, n-1) call. Only when all the recursion calls are complete is the final sum computed. Take a look at how the recursion works by breaking it down step by step:

Summary

Spoiler for this lesson:

``````// Assume the array is integers 1-5, and n is 4, remembering array starts at 0.
// ([1, 2, 3, 4, 5], 4)
function sum(arr, n) {
// leaving out conditional here for brevity
return sum(arr, n - 1) + arr[n];

sum([1,2,3,4,5],4)  // function starts with these values passed to it
arr      n
// we get the following
// first call results in
sum([1,2,3,4,5],3) + 5
// second call
sum([1,2,3,4,5],2) + 4 + 5
// third etc.
sum([1,2,3,4,5],1) + 3 + 4 + 5
sum([1,2,3,4,5],0) + 2 + 3 + 4 + 5
catches if statement
1 + 2 + 3 + 4 + 5

``````

Thank you, @opalko! I think I’m finally getting this.

So calling the function only leaves + 5 (the position of n in the array) and so on until it can’t be called anymore at which point the addition finally happens and the result is returned. The only thing I’m still not quite understanding is why we’re returning arr[0] to escape the recursion. I understand we need a return statement to escape, but why the first position in the array?

It doesn’t have to necessarily be the first position where n=0, but in this case we were following the example of working backwards from n to 0 in this line:

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

We could have started at 0 and worked “up” the array to n but it would require some clever array manipulation and a conditional checking to see if we reached the last item in the array.

`multiply(arr, n) == multiply(arr, n - 1) * arr[n]`

can you please understand me how these two are equal?

2 Likes

There are two different hurdles here.

One is learning to understand recursion (I can deal with that).

The second is understanding how `multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]` is derived.

“. . . notice that `multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]` . . . .”

How is this conclusion/equation reached? What are the steps to find that truth?

I’ve been working on understanding how the recursive function call is working. Here’s how I’m visualizing it:

``````// if var arr = [1, 2, 3, 4, 5];
// when n = 3
// the first recursive call is multiply(arr, 2) * arr[2] =

// restating call one to show values:
// call one: multiplyIterative(arr, 2) * 3 =
// call two: multiplyIterative(arr, 1) * 2 * 3 =
// call three: multiplyIterative(arr, 0) * 1 * 2 * 3 =
// 1 * 1 * 2 * 3 = 6
``````

Is this right?

even until now, I’m still confused. lol

It will be easiest to get help by creating your own topic when you have specific questions about your own challenge code.

The easiest way to create a topic for help with your own code is to click the Ask for Help button located on each challenge. This will automatically import your code in a readable format and pull in the challenge url while still allowing you to ask any question about the challenge or your code.