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: