# How do numbers end up listed in sequential order?

Tell us what’s happening:

If i’m reading the code right, wouldn’t it return [3, 7, 6, 5, 4,] ? Does javascript know to list them in order, or am I reading the code wrong? Just looking for help in understanding WHY the code works in sequential order.

Your code so far

``````
function rangeOfNumbers(startNum, endNum) {
if (endNum === startNum) {
return [startNum];
} else {
var countArray = rangeOfNumbers (startNum, endNum - 1);
countArray.push (endNum);
return countArray;
}
};
console.log(rangeOfNumbers(3, 7));
``````

Your browser information:

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0`.

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

Can you explain how you believe that the array `[3, 7, 6, 5, 4]` would be generated?

1 Like

If you add console.log statements you can see what the function is doing when. Look at the output here and see if you can map what’s going on (in your head or, better yet, in flowcharts on paper):

``````function rangeOfNumbers(startNum, endNum) {
console.log("startnum is "+ startNum + "endNum is "+endNum)
if (endNum === startNum) {
return [startNum];
} else {
var countArray = rangeOfNumbers (startNum, endNum - 1);
console.log("countArray is" +countArray);
countArray.push (endNum);
return countArray;
}
};
console.log(rangeOfNumbers(3, 7));
``````

[startNum… , 3
then, …endNum - 1] , 6
then, .6-1
then, 5-1
thats how it looks visually as I read the code

I see how it start with startNum. How does it get to the sequential oder following that?

I don’t understand what this means

endNum - 1 = 6

code says:
(startNum, endNum - 1)

so, how does it get the next sequential number next and not endNum - 1 next?

the function `rangeOfNumbers(startNum, endNum - 1)` returns an array with the numbers from `startNum` to `endNum-1` in order

than

add `endNum` at the end of that array

my problem is that I know the code works. I am just trying to figure out how to read and execute in my head. I read the code and try to assign it a literal translation.

it starts with startNum, then pushes the endNum next, then endNum - 1 next?

I dont understand the logic in the statement/code.
how am I supposed to read that in my head?

The value `endNum` is different each time the function is called during the recursion.

`rangeOfNumbers(3, 7)` needs to know `rangeOfNumbers(3, 6)`

`rangeOfNumbers(3, 6)` needs to know `rangeOfNumbers(3, 5)`

`rangeOfNumbers(3, 5)` needs to know `rangeOfNumbers(3, 4)`

`rangeOfNumbers(3, 4)` needs to know `rangeOfNumbers(3, 3)`

`rangeOfNumbers(3, 3)` is `[3]`

Therefore `rangeOfNumbers(3, 4)` is `[3, 4]`

Therefore `rangeOfNumbers(3, 5)` is `[3, 4, 5]`

Therefore `rangeOfNumbers(3, 6)` is `[3, 4, 5, 6]`

Therefore `rangeOfNumbers(3, 7)` is `[3, 4, 5, 6, 7]`

2 Likes

Recursion kind of “takes a layer off” and then, at whatever the bottom/interior is, starts adding layers back on. In a general abstract and poorly explained sense. @JeremyLT has explained what the function is doing via the step-by-step method my console.log functions should have shown. Try a smaller range at first, say 3,5 or 3,4 and see if you can figure it out.

1 Like