# Use Recursion to Create a Range of Numbers

Hi, I’m trying to understand this line:

``````      const nums = rangeOfNumbers(startNum, endNum - 1);
``````

I’m trying to figure out why we assign rangeOfNumbers to nums. I console.log’d nums and it returns undefined until numbers start to be pushed onto it. It seems like the nums is only affected by the following line:

``````      nums.push(endNum);
``````

Also, why do we return an empty array at the end?

``````  if (endNum < startNum) {
return [];
``````

Here’s the full code:

``````function rangeOfNumbers(startNum, endNum) {
if (endNum < startNum) {
return [];
} else {
const nums = rangeOfNumbers(startNum, endNum - 1);
nums.push(endNum);
return nums;
}
}
``````

Thanks

First, the easiest thing to answer: You return an empty array `if (endNum < startNum)` because, if that ever happens, the function is getting inputs that it can’t handle. So if you run rangeOfNumbers(2, 1), it’ll return an empty array because the first number has to be less than the second number.

As for what’s happening in the `else` block, that’s the recursion bit… you sort of keep looping through the function until startNum === endNum, then it returns the nums array.

Ya, I get that it’s to stop it and prevent a stack overflow. Just not sure why we return an empty array and not something else or nothing.

And ya, I get what’s going on in the else block in general. I’m just trying to understand it more in depth.

Remember, the function always has to return an array.

Yeah, recursion is one of those things that’s both hard to understand and hard to explain. And I’m not particularly good with the concept myself.

But let me try explaining it a little bit better:

So lets say we pass the numbers 2 and 3 to the function like `rangeOfNumbers(2, 3)` (this is function call #1):

First, it will hit that `else` block. And the first line of the else block is `const nums = rangeOfNumbers(startNum, endNum - 1);` The program needs to resolve this before it can move forward to the `nums.push(endNum);` line.

So now we’re now resolving the function `rangeOfNumbers(2, 3 - 1)` (this is function call #2). Because 2 < 2 is false, we go to the `else` block again. So now we have to resolve `rangeOfNumbers(2, 2 - 1)` (this is function call #3) before we can move forward.

Finally, 2 < 1 is true, so we have an empty array `[]` as the return value from function call #3.

So in function call #2, `num = []`, then we can move to the `nums.push(endNum);` line. In function call #2 `endNum = 2`, so we push that onto num and we end up with  as the return value from function call #2.

So then function call #1 gets  as the value of `num`. In function call #1, `endNum = 3`. So we then push that onto the the num const.

So we end up with [2,3] as the final value of `num` which then gets returned from the original function call.

1 Like

Ya, I just don’t get why. I’m trying to fully understand what’s going on.

What do these two lines do?

``````const nums = rangeOfNumbers(startNum, endNum - 1);
nums.push(endNum);
``````

The `push` method works on arrays, right? So `nums` better be an array, right? So the recursive call to `rangeOfNumbers` better return an array, right?

This stuff is hard, no doubt about it. I’m not trying to poke fun at you here. I’m just trying to help you see the logic behind this.

Ya, I get all that. I get what’s happening, for the most part. I’m just not exactly sure why we return an empty array or what happens with this array, if anything. It doesn’t seem to go to nums. I console.log’d nums and it never seemed to be an empty array at any point. It comes back as undefined until we start pushing numbers to it.

I also don’t get why we assign the function to nums . nums doesn’t seem to have any value until we push each number to it.

Thanks for taking the time to write that all out though.

I dunno. That’s what I’m trying to figure out. I’m starting to think that that the empty array IS being returned nums. How that works, I’m not sure. Intuitively, I would think it would return nums = ; or something like that. I’ll just assume that’s what’s happening.

I’m still not sure what assigning the function to nums does. Now that I think about it, that’s probably why it knows to return the empty array to nums. Maybe that’s all it is, if that is the case. I dunno.

I’m starting to think that the empty array IS being returned to nums, and that maybe it knows to return it to nums because we assign the function to nums. So maybe that answers everything, or at least part of it. I’m still not positive though.

It’s not assigning the function to `nums`. Its assigning the value returned by the function to `nums`. And the function always returns an array.

If you invoke the function with:

``````rangeOfNumbers(7,7);
``````

Remember here that `endNum` equals `7`.
You’ll hit the `else` and the following will execute:

``````const nums = rangeOfNumbers(7, 6);
``````

Before it can go any further than this line it must return a value for `rangeOfNumbers(7, 6)`. You know what that is because that will trigger the base case. So `nums` will be set to `[]`.

Now we can go to the next line, which we can sub in values to make it:

``````[].push(7);
``````

Which is `` and that’s what the original function call returns.

1 Like

Ok, ya, that makes sense. Thanks