# Basic JavaScript - Use Recursion to Create a Range of Numbers

Tell us what’s happening:
I don’t know why it’s not creating an array in else statement

``````function rangeOfNumbers(startNum, endNum) {
if (startNum > endNum) {
return [];
} else {
const arr = rangeOfNumbers(startNum, endNum);
arr.push(startNum, endNum);
return arr;
}
};
``````

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

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

This line creates an infinite recursion.

so, const arr = rangeOfNumbers(startNum, endNum-1);
will make it what?

doesn’t it make the endNum to print a number low in the array??
How is this happening??

Have you tried looking at what output your function is making:

``````console.log(rangeOfNumbers(1, 5));
``````

yes, I did
it is showing like this
[ 1, 1, 1, 2, 1, 3, 1, 4, 1, 5 ]
After adding -1 to endNum in this line → const arr = rangeOfNumbers(startNum, endNum - 1);

Ok, lets dig into what could be doing that.

This is the line that is modifying `arr`

My question is that
if we add -1 to the endNum
why the endNum is not get substracted from the array??

Ex: `rangeOfNumbers(1, 5)` should return `[1, 2, 3, 4, 5]`
How, it is returning 5 also in the array??

This doesn’t change the value stored in `endNum`

okie

The only way to change a value stored in a variable is to use the `=` sign:

If an `=` isn’t used (or an increment/decrement operator https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/decrement-a-number-with-javascript), then the value stored in the variable cannot change.

ok
This is the else block
const arr = rangeOfNumbers(startNum, endNum -1);
arr.push(startNum, endNum);
return arr;

→ I’m unable to pass these test cases

• `rangeOfNumbers(1, 5)` should return `[1, 2, 3, 4, 5]`.

• Failed:`rangeOfNumbers(6, 9)` should return `[6, 7, 8, 9]`.

• Failed:`rangeOfNumbers(4, 4)` should return `[4]`.

• Failed:Global variables should not be used to cache the array.

Right, so coming back to this thing that I said.

What does this line do?

1 Like

It will push the numbers from (starNum) till (endNum) into the array
Am I right??

No, if it did that, then the recursive call would not be needed.

Push adds only the values in the ()s to the end of the array

There is no need to guess what `push` does - you could check this out for yourself.

``````const arr = [];
arr.push(1, 5);
console.log(arr);
``````

figured out the mistake

A very big Thank you for helping me with patience

1 Like

Hi there !
Sorry for jumping in.
We should push(endNum) right?
But as endNum is not reassigned to any other value, how can that push all the other values into the array?

The value of `endNum` never changes inside of a single function call, but different values for `endNum` are passed to each recursive function call.

Got it.
Thanks a lot.

1 Like

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