# Basic JavaScript - Replace Loops using Recursion

Tell us what’s happening:
Describe your issue in detail here.

I used a video to finish the challenge but I still don’t understand recursion. Can someone
explain in detail with examples please.

``````function sum(arr, n) {
// Only change code below this line

// Only change code above this line
}
``````

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

Challenge: Basic JavaScript - Replace Loops using Recursion

Actually doing the challenges will be the best way of understanding the concepts that are going on. The learning experience itself is the only reward you can get on freeCodeCamp that has any real value.

A recursive function is a function that calls itself. It should have a base case (or termination condition) and a recursive step.

Imagine you’re standing at the top of a stairwell with a choose-your-own-adventure book. It reads:

Page 1: If you can go down a step, go to page 2. Otherwise, go to page 3.
Page 2: Go down a step. Go to page 1.
Page 3: The end!

The recursion happens when you go down a step and are sent back to the beginning of the instructions. The base case is when there are no more steps to go and the instructions end.

Now let’s go through a well-known example of recursive code by hand. This is a skill that will help you at any level of software development whenever you need to figure out what a particular bit of code does.

``````function fibonacci(n) {
if(n < 2) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
``````

This function returns the nth Fibonacci number. You can tell that the code is recursive right away because you see its own name, `fibonacci`, inside the function body. Suppose we want to find the 3rd Fibonacci number.
Let `n = 3`:

``````fibonacci(3)
``````

The first thing that happens is a comparison.

``````if(3 < 2) // false
``````

It’s false, so we go to the else part of the if-statement.

``````return fibonacci(3 - 1) + fibonacci(3 - 2);
// return fibonacci(2) + fibonacci(1);
``````

We want to return a value but can’t yet because we need to evaluate the two function calls we just did. We’re going to “step down” into the recursion and do them.

Here’s the first:

``````fibonacci(2)
``````

We plug in `2` to the comparison.

``````if(2 < 2) // false
``````

Else…

``````return fibonacci(2 - 1) + fibonacci(2 - 2);
// return fibonacci(1) + fibonacci(0);
``````

We get the same thing as the last part. We can’t return anything because we have yet 2 more function calls to get through! However, both of them have values that are actually going to pass the comparison.

``````fibonacci(1)
``````
``````if(1 < 2) // true
``````

So since it’s true…

``````return 1
``````

And if we do `fibonacci(0)`, we’ll see that it returns `0`. So if we go one step back up to `return fibonacci(1) + fibonacci(0)`, knowing that `fibonacci(1) = 1` and `fibonacci(0) = 0`, we see that the return statement actually looks like:

``````return 1 + 0; // 1
``````

Great! We got all the way down to the base case and now we’re two steps up from that.

``````return fibonacci(2) + fibonacci(1)
``````

We know that `fibonacci(2)` returns `1` now, because it breaks down into `fibonacci(1)` and `fibonacci(0)` and we know from the if-statement that those just return 1 and 0, respectively.
So since we know both parts of the return statement, we can rewrite it:

``````return 1 + 1
``````

And so, our original function `fibonacci(3)` returns `1+1`, or `2`.
Sure enough, that’s the third fibonacci number.

This example also shows why recursion isn’t always a good thing. If we put in a big number at the start, like `fibonacci(100)`, the code would have to do `fibonacci(99)` and `fibonacci(98)`. Then, it would have to keep going, splitting each one of those two parts into two more parts, and those two into two more each… all the way until the base cases: `fibonacci(0)` and `fibonacci(1)`, and adding them all up from the bottom to the top.

That is a LOT of computation.

Hope this helps. Let me know if you want to talk it through more!

Thank you. I’m going to try and practice nd understand recursion more.

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