I can't figure out why my code doesn't work

Tell us what’s happening:
I’m pretty sure the problem is with the base case, but I can’t figure out what it is. It says Maximum call stack size exceeded, but shouldn’t it have stopped running once endNum is the same as startNum.

code so far**


function rangeOfNumbers(startNum, endNum) {
if (endNum === startNum) { 
  return [];
} else {
var arr = rangeOfNumbers(endNum - 1);
arr.push(endNum);
return arr;
}
}
console.log(rangeOfNumbers(1, 5))

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:

In this line, you forgot to give startNum as the first argument. The rangeOfNumbers() function takes in two numbers remember.

Here, you want to put either the startNum or endNum in the array. Otherwise, there will be nothing in the array being returned.

1 Like

Can you describe with a simple example, e.g. rangeOfNumbers(1, 5) ,
how your code flows?