# Error message in 'Using Recursion To Create Range Of Numbers'

Hi guys, I have looked at the initial solution for this problem and it makes the most sense to me so I broke it down as best as I could to understand it.

My solution is exactly the same as solution one, yet I still keep getting the error message that ‘numbers.push’ is not a function, and I can’t seem to work out why.

Here’s the code.

``````
function rangeOfNumbers(startNum, endNum) {
if (endNum - startNum === 0) {
return startNum;
} else {
let numbers = rangeOfNumbers(startNum, endNum - 1);
numbers.push(endNum);
return numbers
}
};

console.log(rangeOfNumbers(3,6))
``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36`.

Challenge: Use Recursion to Create a Range of Numbers

‘numbers.push’ is not a function,

What that means is that numbers does not have a property called push that is a function.

If I want to see what is happening, I can log out what it is:

``````function rangeOfNumbers(startNum, endNum) {
if (endNum - startNum === 0) {
return startNum;
} else {
let numbers = rangeOfNumbers(startNum, endNum - 1);
console.log(typeof numbers, numbers);
numbers.push(endNum);
return numbers;
}
}

console.log(rangeOfNumbers(3, 6));
``````

This gives me this output:

number 3
Uncaught TypeError: numbers.push is not a function

So, it is correct. numbers is a `number`. That is a primitive so it doesn’t have any properties or methods, but it will box it in the `Number` object and check that, but that doesn’t have that method either. That is what it is telling you. numbers.push is `undefined`, which is not a function.

The push method is found on arrays. Why isn’t this an array?

My solution is exactly the same as solution one,

No, it isn’t. Please recheck. I can a few characters into a line here and it works. I checked and solution 1 has those characters too.

2 Likes

Thank you sir.

From what I can see, the only difference is that I defined ‘numbers’ with let, which shouldn’t be a problem.

And also that in the if statement, startNum was not returned as an array in my solution.

I’m still baffled as to why my solution throws up an error but why the official solution doesn’t.

This is the critical difference, given that `push` is an array method. If it is not in an array, you cannot `push` more numbers.

1 Like

The difference is with the base case of the recursion.
So in your code you have this:

``````if (endNum - startNum === 0) {
return startNum; //here!
``````

The base case of the recursion returns a number. Since all the other function calls will ultimately refer to that, its important that said part is right.
If you just change to an array…

``````if (endNum - startNum === 0) {
return [startNum];
``````

…and examine the solutions offered by the exercise, you see that the only difference to your code is the type of the base case of the recursion, specially comparing to the first solution, which is the closest to your answer.

2 Likes

`.push()` is a method that is available on arrays. Does your `rangeOfNumbers` function return an array?

Thanks guys, I get it now!