Help with 'RangeError: Maximum call stack size exceed'

Hi everyone. I am doing the ‘Create a Range Numbers’ with recursion section, and when I test my code the console says this ‘RangeError: Maximum call stack size exceeded’, and I don’t know why. This is my code:

function rangeOfNumbers(startNum, endNum) {
  if (endNum < startNum) {
    return [];
  } else {
    const countArray = rangeOfNumbers(startNum, endNum - 1);
    countArray.push(endNum);
    console.log(countArray);   // This line is only to see what happens with the array.
    return countArray;
  }
};

rangeOfNumbers(10);

I’ll admit, this one took me a minute to understand.

You are calling this function with only one argument, which means that endNum is undefined.

console.log(undefined < 10);
console.log(undefined - 1);
console.log(NaN < 10);

These three lines might help you see what’s happening.

Omg, now I see it xD. Thank you, I need to put more attention on what I’m doing xd

1 Like

Easy one to miss :slight_smile:

Yeah, I think so. Thanks for help me!

1 Like

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