What is the trouble with this solution?

Tell us what’s happening:

Your code so far

//Only change code below this line
var result=[];
function rangeOfNumbers(startNum, endNum) {
if (startNum <= endNum) {  
else {
  return result;


Your browser information:

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

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

I haven’t looked at the challenge to get any more detail as I think I know the issue already. You are calling your function within itself, you can’t do that:

function rangeOfNumbers(startNum, endNum) {
if (startNum <= endNum) {

How can JS work out the 4th line if you function hasn’t completed yet? see the error?

So there are several tests. They run one after the other. They do not run in isolation, and they run in the same scope that the function + that variable are defined.

For example, first run:

rangeOfNumbers (6, 9)

That’ll give you [6,7,8,9]. That’s correct.

Second run:

rangeOfNumbers(1, 5)

That will not give you what is required for a correct result. What is the return value on this second run?

Think about where you have defined the variable result and what you have coded your function to do.

Note this will happen if you do it in the browser console as well: paste your code in, then just run the function a few times to see what it does

Edit: missed that you you’re not actually returning anything, what @RandellDawson said

Edit edit: although it’ll work with the global variable, it shouldn’t really be there: the function should really be self-contained and not depend something outside of it. So in general, prefer moving the global into the function. Because it’s recursive, you can’t put it in the function body else it will be overwritten every time, so:

function rangeOfNumbers (start, end, result = []) {

result has a default value of an empty array. So
when you first call the function, you don’t need to define it: rangeOfNumbers(6, 9). On the recursive calls, pass in your updated array (the one you’re adding values to). In the terminal call, return result just as you are at the minute.

1 Like

First, you need to make sure you are returning an array when startNum is less than or equal to endNum. The following line needs a return statement:


Also, once you fix the above issue and remove the test call (the rangeOfNumbers(6,9);) from your code , you would pass (even with the global variable).

1 Like