More help with Recursion

Tell us what’s happening:
Ended up having to look at the hints for this challenge. Can some one explain what is happening in this solution. Specifically what is happening here.

var numbers = rangeOfNumbers(startNum, endNum - 1);

What exactly is happening to startNum and endNum This is really confusing.

  **Your code so far**

function rangeOfNumbers(startNum, endNum) {
if (endNum - startNum === 0) {
  return [startNum];
} else {

var numbers = rangeOfNumbers(startNum, endNum - 1);

numbers.push(endNum);
  return numbers;
}
}
  **Your browser information:**

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

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

I recommend asking questions on the forum before resorting to copying the answer.

I have added spoiler tags to this solution.


Recursion is based around reducing the problem to a smaller problem.

In this case, the smallest range we can work with is a range that only has one number in it. So we are shrinking the range in the recursive calls and expanding the result with a push after the recursive call returns.

1 Like

So recursion. It’s a strange concept at first, when we think about it. Write a function that knows enough to call itself, repeating until some end state is reached, then “unwrap” all those nested calls to itself until we’re back where we started, and return a result.

But at a subconscious level, we recurse easily. An example i use often is calculating an “Nth power.” If you needed to solve 3^5, most minds will simply jump to the innermost recursive level needed, 3^2 or 3×3, then return that product to the next level: (3×3)×3, and continue out until we have come back to our original recursive loop value, the exponent.

In the case of an exponent, though, the exponent itself isn’t part of the final result. It simply signifies hour many times we’ll multiply our base value against itself.

In the sum code, the recursion counter is the value being summed. So the operation within the called function is different, but the recursion mechanism itself is much the same.

A more thorough explanation of Nth power recursion (and the “meta” process behind thinking recursively) can be found here: https://tobycodes.tech/posts/recursion-all-the-way-down

For the function call rangeOfNumbers(1, 5), we need to return an array [1, 2, 3, 4, 5]. To think this recursively, we divide this problem into a smaller version of itself. Namely, calling rangeOfNumbers(1, 4) and receive the answer [1, 2, 3, 4]. To this answer, I push 5 to it to get [1, 2, 3, 4, 5].

Now, how does rangeOfNumbers(1, 4) get the answer [1, 2, 3, 4]? Well, this one calls rangeOfNumbers(1,3). Receiving [1, 2, 3], push 4 to it and get [1, 2, 3, 4].

This same logic continues until we cannot break down any further, namely the end case of the recursion. When can we stop recursion? The last call is rangeOfNumbers(1,1). We return [1].

Pictorially, it looks like this

One thing you need to be aware is that although there is just one definition of the function, there are multiple copies, or clones, going on. So, for the first copy startNum is 1 and endNum is 5. For the second copy startNum is 1 and endNum is 4. And so forth.

1 Like