Basic JavaScript - Use Recursion to Create a Range of Numbers

I dont understand why my code is returning all of the numbers except for startNum.

THE TASK The function should return an array of integers which begins with a number represented by the startNum parameter and ends with a number represented by the endNum parameter. The starting number will always be less than or equal to the ending number. It should also work for cases where both startNum and endNum are the same.

function rangeOfNumbers(startNum, endNum) {
if (startNum >= endNum) {
  return [];
let newArr = rangeOfNumbers(startNum, endNum - 1);
return newArr;

console.log(rangeOfNumbers(17, 33));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36 Edg/107.0.1418.56

Challenge: Basic JavaScript - Use Recursion to Create a Range of Numbers

Link to the challenge:

>= are you sure about this condition? I think you are off by one.

Try running your code in this visualizer


OK so I noticed that startNum can be equaled to endNum. The challenge now passes all tests after making that change. But Im not understanding how making that change now allows startNum to be included in the range all of a sudden.

Did you try the tools I posted? Skip to the end and go back a few steps.

1 Like

Sorry but Im just not seeing it. I mean visualizing it.

Think of the number of times the recursive function is called the same way you think about how many times a loop runs. What is the state of the program before the base case and has it reached the expected state (state as in all the variables)?

What happens if the function is allowed to be called one extra time?

Why is endNum not decremented as many times as it needs to be?

When does it get decremented, does the function have to be called for it to be decremented?


OK I get it now. The IF statement was telling the math function where to stop decrementing. Doh. Thanks for your help!

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