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
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.
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.