From the course: Learning Functional Programming with JavaScript ES6+

Recursion - JavaScript Tutorial

From the course: Learning Functional Programming with JavaScript ES6+

Start my 1-month free trial

Recursion

- [Instructor] The next advance concept we're going to look at is called Recursion. Recursion is a concept that you've probably heard of before but maybe you've never seen it in action. What recursion is is simply a case when a function calls itself. While doing this can very easily lead to an infinite loop if we're not careful it can also be used to solve certain problems that aren't easy to solve using other methods. Let's take a look at a very simple example using recursion. We're going to create a function the behaves like a for loop without actually using a for loop. We'll call this function countDown. So const countDown and what this function will do is start at whatever number we pass into it as an argument, we'll call that argument x and it's going to use recursion to countdown from that number to zero printing the results along the way. So all we have to do is print the number, console.log x and then call our function with our argument minus one. CountDown and we'll pass x minus one. If we were to run our code now it will recurs infinitely because we forgot one important thing, in recursion we always have to tell our function when to stop, without a stop condition any recursive function will go on infinitely. Our stop condition in this case will be when the argument that the function receives is less than zero. So up at the top of our function we want to say if x is less than zero and then we simply want to return. This prevents the function from calling itself again at this point. Now if we call our function countDown and will pass in the number 10 and then we run our code we see that it counts from 10 all the way down to zero without using any kind of for loop. So what if we wanted to count up instead? Well this is going to be very similar to counting down with a small few changes. First of all instead of subtracting one from the number on each function call we're going to add one. Second of all instead of checking to see if the current number is less than zero we're going to check if the current number is greater than the number we want to count up to and this means that we need to add another argument. So now x will be the number the we start on and will define another argument called max that we want to count up to. Then inside this if statement we simply check if x is greater than max and if it is then we prevent our function from calling itself again. We also need to make sure to pass this max argument when out function calls itself. And finally let's rename this function to countUp. And when we call this function initially we have to define the number that we want it to start on, x as well as the number we want it to count up to, we'll do 10 again. And if we run our code we see that it prints the numbers from zero to 10 counting up and this is exactly what we wanted. We now have a function that count up without any help from a for loop.

Contents