# Attempted alt solution to "Use Recursion to Create a Range of Numbers"

Tell us what’s happening:
I’m having trouble figuring out why my alternate solution doesn’t work. I first solved it in a way very close to the suggested solution, but then I tried the code shown below under “you code so far,” which doesn’t work. In the normal solutions, the base case simply returns an empty array, and then the function goes back and fills in the recursions, adding numbers to the array. In my failed alternate solution (“your code so far”), I was trying to have the base case already fill in one of the numbers, and then proceed to fill in the rest of the numbers, as in the standard solution. When I expand the recursion on my own in a Word document (to visualize what’s going on), it seems to work, but when I try to “run the tests” the error I get now is “TypeError: array2.unshift is not a function.” Is anyone able to help explain why my attempted alt solution doesn’t work?

Here is my solution that passed, for comparison

``````function rangeOfNumbers(startNum, endNum) {
if (startNum > endNum) {
return []
}
else
{var array = rangeOfNumbers(startNum+1, endNum);
array.unshift(startNum)
return array}
};
``````

And here is my attempted alt solution:

``````
function rangeOfNumbers(startNum, endNum) {
if (startNum === endNum) {
var array = []
return array.unshift(startNum)
}
else
{var array2 = rangeOfNumbers(startNum+1, endNum);
array2.unshift(startNum);
return array2};
};

``````

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

Challenge: Use Recursion to Create a Range of Numbers

Hello, Averroes.

Have a read of the documentation for the use of `unshift`: Mozilla

Specifically, pay attention as to what the method returns.

Hope this helps

1 Like

Oh! I think I got it. The problem apparently wasn’t really the occurrence “array2.unshift”, as the error message led me to believe. When I change line 4 and 5 as follows, it works:

``````function rangeOfNumbers(startNum, endNum) {
if (startNum === endNum) {
var array = []
array.unshift(startNum)
return array
}
else
{var array2 = rangeOfNumbers(startNum+1, endNum);
array2.unshift(startNum);
return array2};
};
``````

So the strategy as a whole seems viable, and the error was earlier than I thought. I assume that is what you were pointing me to, Sky020? That I needed to first simply perform the unshift in line 4, then return the new array, because otherwise I’m just returning “the new length of the array”, as the documentation you pointed me to indicates? That earlier error must have messed things up from the outset, such that I eventually receive the "“TypeError: array2.unshift is not a function.” But I imagine understanding that fallout is above my ability right now.

Thanks for your help, in any case!

here you store the output of a function in a variable and then use `unshift` on it assuming that output is an array
if that output is instead a number (because that function returns `array.unshift(...)`) then you get that error because a number doesn’t have the `unshift` method

Oh interesting. That makes sense. Thanks!