Challenge: Use Recursion to Create a Range of Numbers

Recursion can be a bit tricky, and I understand exactly what’s happening in this challenge. However, I don’t know how the end of the challenge makes sense. I know that the end nums are in a callstack, therefore you must return that later. But, i thought that once you return a function its stops. My question is once you return (endNum - startNum === 0) {
return [startNum];
Why does it continue to call the function?
My second question, when you push the endNum to the array, how does it know to push those nums to that array?
And at the end when you are pushing these numbers to the array, what does the function look like when you are calling it? For example const numbers = rangeOfNumbers(endNum =7)?

Hi and welcome to the forum. Can you post some code to help us better discuss what you mean?

Each function call is separate. In the base case, when endNum - startNum === 0, the function call for the base case returns, but the other function calls that preceded the base case still need to finish and return.

function rangeOfNumbers(startNum, endNum) {
  if (endNum - startNum === 0){
    return [startNum]
  }else{
  const num = rangeOfNumbers(startNum, endNum - 1)
  num.push(endNum)
  return num
  }
};
console.log(rangeOfNumbers(7, 9)).

Oh okay, so what you’re saying is even after returning the function, the other function calls that were called before need to finish. Therefore we call it the function again until they all return?

Thanks for answering the first question, that makes sense now. Can you proceed to the second and third questions? :pray:

I’d try adding these console logs to your function

function rangeOfNumbers(startNum, endNum) {
  console.log("startNum = " + startNum);
  console.log("endNum = " + endNum);
  // ...
    console.log("returning " + num);
};

That should help with your third question.


I don’t understand your second question. The line

  const num = rangeOfNumbers(startNum, endNum - 1)

returns an array, which you are saving to the variable num. You are pushing onto that array.

I guess I just don’t know how the returned array is linked to num. But I am assuming once you return the array [startNum], num is now the returned array, which means you can now push the endNums to it?

I think I’m echoing your answer, but I just want to make sure

The function call

rangeOfNumbers(startNum, endNum)

always returns the array

[startNum, startNum + 1, ..., endNum]

So, the function call

rangeOfNumbers(startNum, endNum - 1)

will return

[startNum, startNum + 1, ..., endNum - 1]

(num is perhaps a little bit confusing of a name for it)
This is the array that you’re pushing endNum onto.

1 Like

Thanks, I understand now, they’re basically just several function calls waiting to be returned.

This is a good code to try:

function countup(n) {
  console.log("function call: countup(" + n + ")");
  if (n < 1) {
    console.log("  base case");
    console.log("  returning: []\n");
    return [];
  } else {
    console.log("  recursive case");
    console.log("  calling countup(" + (n - 1) + ")\n");
    const countArray = countup(n - 1);
    console.log("back to function call countup(" + n + ")");
    countArray.push(n);
    console.log("  returning: [" + countArray + "]\n");
    return countArray;
  }
}

countup(5)

This produces:

function call: countup(5)
  recursive case
  calling countup(4)

function call: countup(4)
  recursive case
  calling countup(3)

function call: countup(3)
  recursive case
  calling countup(2)

function call: countup(2)
  recursive case
  calling countup(1)

function call: countup(1)
  recursive case
  calling countup(0)

function call: countup(0)
  base case
  returning: []

back to function call countup(1)
  returning: [1]

back to function call countup(2)
  returning: [1,2]

back to function call countup(3)
  returning: [1,2,3]

back to function call countup(4)
  returning: [1,2,3,4]

back to function call countup(5)
  returning: [1,2,3,4,5]

Everything is just qued in the stack then waiting to be returned?

1 Like

Yep. In the above, countup(5) can’t proceed and eventually return an array until countup(4) fully finishes.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.