hey there, I’m not the best with recursion but if I had to guess I would say you are failing to reach your base case. I can see that on the last call of your function you are expecting to return an array, so you would expect push to work. Also, think about whether push is the best function to use here, if you think about it you will end up nesting a bunch of arrays inside an array instead of joining them together.
Also I would reconsider what your base case is returning. Your base case should happen with the simplest possible input which in this case is two of the same number. What do you want to return if you have two of the same number?
the reason you get the error its not a function is because countArray.push(endNum); is an array, so you would just be returning the array with endNum pushed on the end so recursion will end.
actually I just pasted that in the challenge and it passed, I’m not seeing any error. It’s not how I solved it myself but it seems to work fine for me. I tried it in the console and it worked fine with no errors.
Found the answer:
countArray.push(endNum); will return the length of the array after push.
if you do “”“return countArray.push(2,3,4);”“” it will return 3 which is the length of resulting array after push method. If you do “”“return countArray”“” we are returning array not just length of array.
proof:
Why would it end:
countArray = rangeOfNumbers(startNum, endNum - 1)
which means it will return:
rangeOfNumbers(startNum, endNum - 1).push(endNum)
and the function will call itself again, can you explain more please?
I got it…
I want you to figure out by yourself…
so take out your pen and paper, and assume some value for startNum and endNum, such as 2 and 5…
Manualy follow your own code and remember Array.push() method returns a number not an array…
If you still have problem with recognising the error, just let me know…
tbh after reading this i still dont understand why it works like this lol javascript is such a confusing language and you learn something new everyday.
lets take 2 as startNum and 5 as endNum:
/*we call the function–>*/rangeOfNumbers(2, 5);
endNum > startNum
countArray = rangeofnumbers(2, 4);
return rangeofnumbers(2,4).push(5) /***it will return countArray which is rangeOfNumbers of endNum - 1 ie :rangeofnumbers(2,4) ***/
and by repeating itself , the result would be like that
return [].push(2).push(3).push(4).push(5)
return [2,3,4,5]
that what i think, correct me if i am wrong
Thanks for dedicating your time and helping to explain, i really appreciate that, i did watch the video and became more confused ,
Now can you check if i am already understanding things correctly:
-[].push(1,2,3) make the array look like that : [1,2,3]
-return [].push(1,2,3) make the array look like that : [1,2,3] plus returns the length of the array.
-The function works like that: For example: rangeOfNumbers(1, 3) ---> [].push(1).push(2).push(3) and after processing we have [1,2,3]
after running "return countArray.push(endNum)" in recursion mode you will have this:
first : “countArray” gets [ ] because you call next “rageOfNumbers” functions, where they are equaled to “countArray”. then: it comes back to a higher level and with that if statement you set, push the new number to [ ] and now your array is [number] (not an empty array). then: but because in the previous step you just return push method id didn’t return an array to use in a higher level of recursion and in next steps we just have a number that returns from push method which we can’t invoke push method on a number and we get an error.