# Replace Loops using Recursionv

Tell us what’s happening:
Hello, I trying to fix this lesson and understand exactly what are going in the code.
I did until here, my doubts is about the “else return”. For what I understood, I need return the sum with the first arr[index].

``````  **Your code so far**
``````
``````
function sum(arr, n) {
// Only change code below this line
if ( n <= 0) {
return 1;
} else {
return sum += arr[0];
}
// Only change code above this line
}
``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36`

Challenge: Replace Loops using Recursion

Link to the challenge:

You need the first `n` numbers of the array added together. So if `n` was 5, and the array was `[1,2,3,4,5,6,7,8,9,10]`, we would want 1+2+3+4+5. If `n` was 3, we would want the first 3 numbers added together, not just the first number in `arr`.

The other thing is you need to call the `sum` function recursively. So your `else return` needs `sum(arr, n)`, not just `sum`. And `n` eventually has to reach `0` . The second example in the test is closer to what you need, but you will need to change things around for it to work.

1 Like

Thanks for your help. I did the modifications that I think and I got to this code, i know its wrong but i understand about calling sum recursively. My question now is the second parameter to do the sum.

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

Remember that our base case is `n <= 0`. When you keep calling sum with the same value of `n`, will `n` ever equal zero?
And we eventually want to add all the `n` numbers in the array together, is `n[0]` our array numbers?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.