Ughhh still dont understand recursion

Tell us what’s happening:
Describe your issue in detail here.
Im looking at the solution in this problem… how can we call .push() on a variable that we didnt initialize as an array?

  **Your code so far**

function rangeOfNumbers(startNum, endNum) {
if (startNum === endNum){
  return [];
  } else { 
    return 
  }
// return an array of ints which begins
// with a num represented by the startNum param
// and ends with endNum param 
};
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

The return value of rangeOfNumbers is always an array, so you can always push onto the result from calling rangeOfNumbers.


FWIW, I’d stop looking at the solutions right now. That will just make it harder for you to successfully complete the projects.

1 Like

Imagine you’re the one handling rangeOfNumbers(1, 5). Inside your else, you call your clone that handles rangeOfNumbers(1,4). Your clone rangeOfNumbers(1,4) will give you back an array [1, 2, 3, 4]. You push 5 to it to create [1, 2, 3, 4, 5]. You’re done with your work, so you return your answer [1, 2, 3, 4, 5] to your caller .
Now imagine you’re the one handling rangeOfNumbers(1,1). You don’t need your clone to help you because you can answer it immediately (this is the end case). You just return XX. In your given code, you’re returning an empty array. You want to check if that is really the case.

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