# Basic JavaScript - Replace Loops using Recursion

Can anyone help me understand why this works. I don’t get how this function is going through the array and adding up the first two numbers in the array? It’s not a loop, so how does this go through every number until it has gone through n numbers? I feel as though it should add the value of the nth number in the array to all of the array. So I would expect sum([2, 3, 4], 1 to return [4, 5, 6], 2. Am I missing something obvious?

``````function sum(arr, n) {
// Only change code below this line
if (n<=0) {
return 0
}
else {
return sum(arr, n-1) + arr[n-1]
}
// Only change code above this line
}

console.log(sum([2, 3, 4], 1))
``````

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

Challenge: Basic JavaScript - Replace Loops using Recursion

Put some real numbers in there. What happens when you call the function with:

``````sum([1,2], 2)
``````

Don’t just tell me what the answer is, we already know that. Explain in detail what happens inside the function. I’ll get you started. Since `n` is greater than `0` you will hit the `return` statement in the `else` block. What does that return?

1 Like

since 2 is greater than 0 it will move to the else statement. My problem is I don’t get how that works. It looks like it should return the whole of the array + the value of arr[n-1]. So, I would expect sum([1, 2], 2) to return 3, 4 or [3, 4], 3 by adding 2 to 1 and 2. I know that it doesn’t do that, it returns 3.

Sub in the real numbers. When you hit that `return` statement in the `else` block, what does it actually return? Based on what I gave you, put in the actual numbers in that `return` statement. Don’t try to go any further at this point, just show me what is actually being returned in that statement.

1 Like

Ok, sub the variables with the values you gave me. So, return sum([1,2,] 1) + 2.
I’m sorry but that is what is confusing me. It looks like I am adding everything by arr[n] which is 2.

Excellent. So now you have a function call to `sum` and then you will add the result of that function call to `2`. So now what happens when you call:

``````sum([1,2,] 1)
``````
1 Like

I am adding 2 of the sum arr values?

You don’t need to guess here. You wrote the `sum` function, so you know exactly what it does

Just like we did before, explain to me what happens when you call

``````sum([1,2] 1)
``````

Don’t worry about the `+ 2` from the original function call. It will always be there. But we can’t add anything to that `2` until we know what `sum([1,2] 1)` returns.

1 Like

it checks if n is greater than zero

Right, and in this case it isn’t, so what does it return? You need to write it out just like you did before. Replace variables with actual numbers, just like you did before.

1 Like

This isn’t correct. Forget about the original function call. We already know what that returns

``````return sum([1,2] 1) + 2
``````

The `+ 2` will be there forever, waiting for us to figure out the other part of the `return`, which is what I am trying to get you do do

So again, what does the following return?

``````sum([1,2] 1)
``````

I don’t want to see you mention ` + 2` again

P.S. Please don’t delete posts that I’ve responded to. It makes it hard for other people to follow this thread. It’s OK to make mistakes as you are learning. That’s how you learn.

2 Likes

My response to

it will return sum([1,2], 1) + 2 inside of the original call. So its really (sum([1,2], 1) + 2) + 2,

No, it isn’t. You are getting ahead of yourself here.

You really should ignore the +2 and focus on what the function call `sum([1,2], 1)`

1 Like

Ok it just clicked. sum([1,2], 2) returns sum(1,2] 1) because we are subtracting 1 from n in the else statement. So sum([1,2] 1) returns sum([1,2] 0), right?

Also thank you very much for you patience and explanations.

I think you are getting close but I’m not sure you’ve convinced me that you have it 100%. But perhaps I am not reading your explanation correctly?

If we call `sum([1,2], 1)` then we hit the `return` statement in the `else` block since `n` is greater than 0. So what would that `return` statement be if you replace the variables with the actual numbers? You are correct, it would include `sum([1,2] 0)` but you are missing something else.

I definitely don’t have it 100% down, I was just very confused for about what sum(1,2] 1) would return. I don’t know what else would be included in besides sum([1,2] 0). is it zero, since it fails the if (n<=0)?

I’ll just reiterate, you wrote the function, so you have the function code right in front of you and thus you know what it will return. Since `n` is greater than `0` then you know that you will hit the `else` block and execute that `return` statement. You just need to plug in the actual numbers for the variables in the `return`. Please do that. What gets returned for the function call `sum([1,2], 1)`?

sum([1,2], 0) + arr[0] or sum([1,2], 0) + 1

Perfect. So now we can go back to the original function call. Remember that `sum([1,2], 2)` returned

``````return sum([1,2] 1) + 2
``````

So now that we know what `sum([1,2], 1)` returns we can plug its return value in there. So now the original return can be written as

``````return sum([1,2], 0) + 1 + 2
``````

Do you see how that works? There is no magic going on here. We are merely replacing a function call with it’s return value. In this case we have replaced `sum([1,2] 1)` with `sum([1,2], 0) + 1`.

Now, let’s continue. What does `sum([1,2], 0)` return? This time `n` does equal `0` so this should be straightforward.

it returns 0, so if I plug that in to

I should get return 0 + 1 + 2