Basic JavaScript - Use Recursion to Create a Range of Numbers

Tell us what’s happening:

I have solved the recursion problem, but I do not understand why endNum has to be first in the if statment? I am sure it is listed somewhere in the previous lessons but I have not found it.
// Correct block of code example.
function rangeOfNumbers(startNum, endNum) {
if (endNum < startNum) {
return ;
// incorrect block of code example.
function rangeOfNumbers(startNum, endNum) {
if (startNum < endNum) {
return ;

Your code so far

function rangeOfNumbers(startNum, endNum) {
  if (endNum < startNum) {
    return [];
  } else {
    const numbers = rangeOfNumbers(startNum, endNum - 1);
    numbers.push(endNum);
    return numbers;
  }
};
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/125.0.0.0 Safari/537.36

Challenge Information:

Basic JavaScript - Use Recursion to Create a Range of Numbers

What would happen if you flipped the order?

Hi JeremyLT. the console would return an empty array. depending on the rangeOfNumbers I would use except 4, 4 which is maxium call stack size exceeded.

That phrase doesn’t make sense to me? I’m not sure what you mean.

If you flip the condition, you have startNum < endNum. Would you expect this to almost always be true?

Nope, lol! Got it. Monday morning brain. The Maxium Stack size exceeded message is win I would console.log (rangeOfNumbers(4, 4)) with startNum < endNum. instead of the actual correct code. The rest of the range of numbers I tried would give empty array. Think i get it now. Thank you.

2 Likes

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