# Recursion Clarification

Tell us what’s happening:

I am having trouble with how recursion works in general and specifically in the problem included. I understand that instead of using loops, recursion calls itself to return the value. I just don’t understand how that value is being calculated. This line is where I am confused:

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

Parameters for reference:
console.log(sum([2, 3, 4, 5], 3));
``````

I understand the second half

``````+ arr[ n - 1 ]
+ arr[3 -1]
+ arr
+ 4
``````

But how is the following evaluated? Why does it equal 2, 3 or cumulatively 5?

``````sum(arr, n - 1)
``````

Is there something I am missing?

``````
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, 5], 3));
``````

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

Challenge: Replace Loops using Recursion

This image might help. It seems counterintuitive at first, and it will for a while. Basically, it’s going to keep counting down from 3 until it hits zero. Once it hits zero, the conditional states it’s not going to call function `sum()` again, and instead it’s going to `return 0;`, thus ending this recursive cycle. Once it has that concrete number, 0, it can go back and “complete” all the other functions waiting for a result, and it ends up adding them together.

Basically, do “x” until “y” happens. “x” just happens to be a function. Once “y” happens, every instance of “x” will get its answer in a cascading fashion.

Keep in mind this image does not represent your problem exactly, but it’s very similar.

Maybe I am missing something but isn’t that image just showing how you would iterate through an If Else loop? How does

``````sum(arr, n - 1)
``````

iterate? I thought the idea of recursion was to omit looping? Am I wrong?

Edit: I think I get it,

``````sum(arr, n - 1)
``````

is used the same way that we would use

``````n < arr.length;
``````

in a For Loop. It’s used as a way to iterate through all conditions that are truthy and the values for each iteration and evaluated by

`````` + arr[ n - 1 ]
``````

When it finally iterates down to n === 0, it then sums all of the evaluated data and returns the value. Is that correct? In this example 4 + 3 + 2 + 0 === 9

Yep, you got it. All it’s doing in the lesson and in my example is essentially replacing a loop. Sometimes it can be pretty useful it’s it’s a complex loop, but usually it’s just easier to write a for or while loop.

1 Like

Recursion seems a lot more complicated that the other type of loops, how often would you say that you use recursion? Do you know of a good resource that I could use for more information? I have a couple courses on Udemy but I haven’t seen any information on recursion.

I rarely use recursion. A lot of the time it’s the programmer’s choice. For more info just google javascript recursion, eg. There are some examples you can find, but there’s really never a scenario where you always or should use recursion.