Use Recursion to Create a Range of Numbers Challenge

Tell us what’s happening:
So, I am not understanding how the function is working.
For example:

if rangeOfNumbers(1,5) it returns [1,2,3,4,5]. How is the 5 being added, when the endNum is being reduced by 1 and then numbers.push(endNum) occurs?

If, ‘push’ is being used then would it not look more alike [5,4,3,2,1]?

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;
}
};


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/84.0.4147.89 Safari/537.36.

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

else {
  var numbers = rangeOfNumbers(startNum, endNum - 1);
  numbers.push(endNum);
  return numbers;
}

endNum itself is not being reduced by 1, the value of the second argument to rangeOfNumbers() is being reduced by 1. The first time you call this function you hit the else block. If we show the actual values being used it looks like:

var numbers = rangeOfNumbers(1, 4);
numbers.push(5);

Passing endNum - 1 to rangeOfNumbers does not reduce the value of endNum by 1, thus we are still pushing the number 5 onto numbers.

The key here is that numbers.push(endNum) is not called until after the recursive call to rangeOfNumbers. In the original function call we hit the else block and the first line evaluates to:

var numbers = rangeOfNumbers(1, 4);

We have a recursive function call, so we don’t go any further in the else block, we have to evaluate this function call first. So we call it for a second time (with the args above) and we hit the else block again and the first line evaluates to:

var numbers = rangeOfNumbers(1, 3);

Again, we can’t go any further than this because we have to call rangeOfNumbers again. Each time we make the recursive call we are stopping the current function call from proceeding to the push because we have to wait for the recursive call to return a value. We keep doing this until we hit the base case rangeOfNumbers(1, 1) at which point we can finally return something. For the base case here we would return [1].

Now we can unravel the recursion. The recursive function call before the base case is waiting at:

var numbers = rangeOfNumbers(1,1);

Since we have a value for the base case we can rewrite it as:

var numbers = [1];

And now we can execute the push. Remember that the recursive call rangeOfNumbers(1,1) is passing in endNum - 1 as the second argument, but endNum still equals 2 in the function that is waiting for the base case to return, so the push becomes:

numbers.push([2]);

And we know that numbers = [1] so we can rewrite that as:

[1].push([2])

which is:

[1,2]

And this is the value we return to the recursive function call that is waiting one level higher in the call hierarchy. Keep repeating this process until we get back to the original function call and we have:

var numbers = rangeOfNumbers(1, 4); // numbers = [1,2,3,4]
numbers.push([5]); // [1,2,3,4].push([5])
return numbers; // return [1,2,3,4,5]
1 Like