# Basic JavaScript - Replace Loops using Recursion

Tell us what’s happening:
I have gone ahead and looked at the solution but I just can’t get my head around what is happening at the ’ return sum(arr, n - 1) + arr[n - 1];’ section. So for example if: arr = [1,3,4,5] and n = 2, then at the return statement what is actually happening? I can’t work out how the computer is dealing with those numbers if that makes sense? I’ve looked at several guides and videos on recursion and even revisited how functions work but I just can’t understand what is going on ‘behind the scenes’ at the return bit of this particular example. Greatly appreciate any help, thanks.

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

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

Challenge: Basic JavaScript - Replace Loops using Recursion

You can start by replacing the variables in the return statement with their actual values. So if you called sum as:

``````sum([1,3,4,5], 2);
``````

Then replace the variables in:

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

With their actual values.

2 Likes

Thanks for the reply. I can’t even seem to think how that would work? Like this?

``````sum([1,3,4,5], 2 -1) + 3
``````

I still don’t know what is happening there. So the ‘+ 3’ bit is because 3 is the 2nd element of the array. But what is being added to the 3 there?

Well, let’s look at the return statement (I shortened `2-1` to `1`):

``````sum([1,3,4,5], 1) + 3
``````

So what is this?

``````sum([1,3,4,5], 1)
``````

It should look familiar

Let me clarify. I don’t want you to tell me the value of it. You don’t need to figure that out yet. I just want you to explain what it is.

2 Likes

It’s the original function with 1 instead of 2 as ‘n’. Thanks again😵‍💫

That’s right. So now you need to find the value for that function and then add it to `3`. And you keep doing this process until you get to the base case.

1 Like

Right, but sorry that’s my original question. As far as I can see the function never returns a value to work with because it’s always jumping to the ‘else’ block (which then just sends it back to itself with an infinite loop as far as I can tell)…

OK, let’s pick up where we left off:

``````sum([1,3,4,5], 1) + 3
``````

So we know we will add `3` to the return value of `sum([1,3,4,5], 1)`. So what does `sum([1,3,4,5], 1)` return? Don’t guess. Don’t tell me what you “think” it returns. It’s a function call to `sum` just like `sum([1,3,4,5], 2)` was, so you should be able to tell me exactly what it returns by looking at the definition of `sum`.

1 Like
``````sum([1,3,4,5], 1) + 3
``````

Ok it returns the function ie: it takes [1,3,4,5] as the 'arr ’ and 1 as ‘n’ and sees that 1 is not less than or equal to 0, so it jumps to ‘else’ which subtracts 1 from 1 giving us 0…

This time we have [1,3,4,5] as ‘arr’ and ‘0’ as ‘n’. Because n = 0, it returns 0. So after all that, we have 0… which is obviously isn’t right. And confusion intensifies

You jumped too far ahead. Just show me the actual code of the return statement for `sum([1,3,4,5], 1)`. Replace all of the variables with their actual values. Just like we did before for `sum([1,3,4,5], 2)`.

1 Like

So the return would be:

`[1,3,4,5], 0`

Are you sure about that? I’m looking at the code for the function and the `else` block is:

``````else {
return sum(arr, n - 1) + arr[n - 1];
}
``````

What you typed above doesn’t seem to include everything in that `return` statement.

`[1,3,4,5], 0` + 1 ???

Look at the `return` statement in the code again:

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

Does your answer have everything in that `return` statment? Look above for what `sum([1,3,4,5], 2)` returned. You’ve done this before. I’m just asking you to do the same thing again, except this time the second argument is `1` instead of `2`.

I know I sound like a broken record, but I feel like you are missing an important concept in recursion and I am trying to make you realize what it is.

1 Like

Oh sorry I see, so it’s:

``````sum([1,3,4,5], 1) + 3
``````

?

I understand your approach, thanks for persevering with me!

Wait, I was asking you to tell me what `sum([1,3,4,5], 1)` returns and you think it returns `sum([1,3,4,5], 1) + 3`? That’s what `sum([1,3,4,5], 2)` returns. Remember? We figured that out above.

You still have not told me what `sum([1,3,4,5], 1)` returns. You know it is going to hit the `else` block`and thus will execute this`return` statement:

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

All you need to do is replace the variables with their actual numbers, just like we did before. I’m sure you can do it.

What does `sum([1,3,4,5], 1)` return?

1 Like
``````sum([1,3,4,5],0)
``````

?

Again, look at the actual `return` statement for the function in the `else` block (I pasted it again in my previous post). So if we called the function as `sum([1,3,4,5], 1)` what is that `return` statement going to be? All you have to do is replace the variables in the `return` statement with their actual values based on the values passed into the function when we call it with `sum([1,3,4,5], 1)`.

There is no need to be guessing here. You have already done this with the original function call of `sum([1,3,4,5], 2)`. Do you remember that? I asked you up above to replace the variables in the `return` statement with their values and you did that. If you don’t remember then scroll above to find it. Perhaps that will help you answer the question of what `sum([1,3,4,5], 1)` returns? Although you really shouldn’t need to look above because you have the code for the `return` statement and you just need to replace the variables.

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

If we call the function as `sum([1,3,4,5], 1)` then inside the function, what does `arr` equal? What does `n` equal? If you know that then you can replace the variables with their actual values and you can tell me what it returns.

1 Like

`sum([1, 3, 4, 5], 2)` returns `sum([1, 3, 4, 5], 1) + 3`
`sum([1, 3, 4, 5], 1)` returns `____________________ + _`

Fill in the blanks here just as you did the first time!

1 Like
``````sum([1,3,4,5], 0) + 1
``````

The 0 comes from 1 (n) minus 1

The ‘+ 1’ comes from the 0 (1 or n minus 1) element of the array
?

Thanks both