Range of number function

Tell us what’s happening:
Please can someone explain why this code doesn’t display an array?

  **Your code so far**

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

};

 
  **Your browser information:**

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

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

I’ve seen many similar questions on this forum related to this, just search “range of numbers” on the search bar here. On other forums people won’t welcome you for duplicate questions.
and by the way, look at this call in your code rangeOfNumbers(startNum - 1);, you have 2 parameters for the function, and endNum should be decremented and startNum be the same

1 Like

Two first checks seems fine.

Recursive call is missing one value.
Also, the way it is written will result in an infinite loop if startNum < endNum.

1 Like

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