Hey all,

I wanted to share this, as I read through the previous posts and nothing mentioned my problem with understanding how this all works.

Here is my code:

function sum(arr, n) {

// Only change code below this line

if (n <= 0) {

```
return 0;
```

} else {

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

For anyone still stuck or for those who come across this looking for clarity, here is a breakdown of what is happening:

First, let’s break this down from the beginning.

function sum(arr, n) {

- The
*arr*is an array that the user will be inputting with the function. - The
*n*is, and this is important, the number of**elements**we are asking to sum in*arr*. - This means that whatever
*n*is, will refer to the**number of elements**we want to sum in the array.

if (n <= 0) {

```
return 0;
```

- This is saying that if the value of
*n*is less than or equal to 0, return the value of 0.

else {

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

This was the tricky part for me, so I will break this into parts:

*return sum(arr, n - 1)*

- This is calling the function of summing the array
**within**the number of elements,*n*, assigned. So for example, if n = 1, this will set*n - 1*to equal 0, meaning that 0 elements will be summed from this portion of the equation. If n = 3, the value of n - 1 would be 2, so the**first 2 elements**in*arr*will be summed.

*arr[n - 1]*

- This just refers to the index of the array at the
**value**of n - 1. So for the example, if n = 1, the value of this equation would be 0, so we would use the index of 0 in the array. If n = 3, the value of this equation would be 2, so we would use the index of 2 in the array.

Put this together and you get a function that sums the elements in *arr*. First, it checks to make sure that *n* will not equal 0. Then it firsts gathers the number of elements to sum, then it adds that sum to the final index value of whatever *n - 1* is. My problem was assuming *n* was the index, and I kept confusing myself.

Hope this helps.

**Your browser information:**

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0`

**Challenge:** Replace Loops using Recursion

**Link to the challenge:**