Why is the call stack size being exceeded?

As far as I understand, countNum should be queuing endNum - 1 to the bottom of the stack until it hits the “base case”. This code isn’t that different from the prior lessons. Is my logic at fault here? Thanks in advance for any help.

Code
function rangeOfNumbers(startNum, endNum) {
if (endNum < startNum) {
  return [];
} else if (startNum > endNum) {
  return "Pick a starting number higher than the ending number";
} else if (startNum === endNum) {
  return startNum;
} else {
  const countNum = rangeOfNumbers(endNum - 1);
  countNum.push(endNum);
  return countNum;
}
};

// RangeError: Maximum call stack size exceeded
console.log(rangeOfNumbers(1, 10)); 

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

The function rangeOfNumbers takes two arguments. There are a couple of other issues, but this one is why you are blowing the stack.

2 Likes

if you call the function like this, what are the values of startNum and endNum and the result of endNum < startNum and startNum > endNum and startNum === endNum?

Also, aren’t endNum < startNum and startNum > endNum doing the same thing?

3 Likes

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