Conclusion

React Native Stack Navigation is a navigation system that allows you to navigate between screens in a stack. The stack navigation system is built on top of the React Navigation library and provides a…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




An Introduction to Recursion in JavaScript

In my journey toward better understanding and use of algorithms, I have most recently decided to conquer recursion. At its simplest, recursion is a technique of calling a function from within itself. Recursion is not only a technique of computer science, and it was a mathematical pattern long before computers existed.

In fact, the recursion people might be most familiar with is the Fibonacci sequence, where the next number in the sequence is determined by the sum of the previous two numbers. The sequence is seeded with two initial values: zero and one. From that point, the recursion can be used to generate the sequence ad infinitum:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144…

Since this is a simple and well-known recursion, I will use it as my first example of writing a recursive method. First, one must decide if the function needs any inputs and whether there are any special conditions. For the Fibonacci sequence, we know that the zeroth and first members are zero and one, respectively, so we can set those as special conditions on our function for finding the nth member of the Fibonacci sequence.

Now, if we ask our function for the zeroth or first members of the sequence, it will return zero or one as expected. But how to return numbers at higher indices? The answer is to call the function on previous output of the function — that is, call the function on earlier members of the sequence. To diagram this out with pseudocode:

Now let’s write that in functioning JavaScript:

Now, our function will recur (call itself) until it calculates the Fibonacci number at index n. Calling our function on an n of four produces the number three, which is the fourth-index member of the Fibonacci sequence:

This is good, but there are lower time-complexity ways to perform simple operations like finding the nth member of a sequence. What if I need to do something more complicated?

A very common coding problem asks us to find the number of combinations given a set of conditions. For example, find the number of unique combinations of length k given a range of numbers [1,n]. For this problem, order does not matter, meaning that [1,2,3] is considered the same as [3,2,1]. How can we use recursion to solve a problem like this?

The first step is to decide what likely inputs the function will need in order to be called.

A person calling this function would only need to provide n and k since the other three variables are given default values in the function’s argument list. The next step is to think about what we want the function to do. We know we need to make combinations including each number in the range (represented by index), so it’s a likely bet that we will need a loop to accomplish this action.

This for loop moves the index across each of the numbers in the range, finding the combinations at each one. Within the loop, comboFinder calls itself to add numbers other than the index into the temp array. But as the code stands right now, nothing is adding to the returnArray, and nothing is limiting the temp array to the length k. So we need to add some code to add temp to returnArray once it reaches a length of k, and then reset it for more looping.

It is important to note that a feature of the JavaScript language is at play in the if statement here — that is, if an entire array is pushed onto another array, as in the if statement, the array being pushed is destroyed because JavaScript’s push function is destructive. This is important to the comboFinder function because we want temp to be cleared down to its empty value after reaching a length of k and being added to returnArray. The function is now only missing one element for success: the return value. We need to return returnArray at the end of the function, or nothing will happen.

And that’s it. Now our comboFinder function can accept a range maximum value of n and find all unique combinations of length k in that range. Elegantly, the solution also handles edge cases such as n being one or k being one.

Recursion, though a simple technique to explain, is very difficult to employ. At its heart, a recursive function is simply a function that calls itself, but as we have seen in only two examples, it can get much more complicated than that. Knowing when and how to call the function within itself only comes with practice and exposure to recursion problems. Sometimes, it is best to avoid recursion altogether since it introduces exponential time complexity. In more difficult problems, however, that may be much less than the complexity introduced by the multiple loops that would otherwise be needed to find a solution. Recursion is a useful and elegant technique essential to the toolkit of the software engineer.

Add a comment

Related posts:

7 Surprising Things In The Janet Jackson Documentary

There is no introduction I could write that would be worth of Janet Jackson. My childhood hero. I remember watching the MTV Jams countdown ONLY for her videos. All of my school friends were Mariah…

Confessing My Crimes

I was a mean-spirited, rebellious, jealous, fragile selfish brat. Why is my life so fucked up? Why are my relationships so fucked up? How do I fix my life after so many failed relationships? How do I date again after being in bad relationships? How do I heal from bad relationships?

A New Perspective at Grace Hopper

Hi HackHer413 community! I’m Arianna, a senior Biochemistry and Informatics major at UMass Amherst with the goal of working in bioinformatics. This October, I and a lucky group of 9 other UMass…