Can someone please explain what each of the 3 lines of code within the else statement does please. How does countArray.push(n); work if countArray is not an Array? Does return countArray; recall the function and return the current array? Also, if possible, can someone please expand the line: return countArray;.
Let’s use a very simple example. What does the following function call return?
countup(0);
Since n is less than 1 then the countup function will return an empty array ([]).
Now let’s look at another simple example:
countup(1)
Since n is greater than 0 we will hit the else block and do the following:
const countArray = countup(0);
We pass 0 into countup because n-1 in this case is 1-1. And we already know what countup(0) returns, right? It returns an array. It may be an empty array, but it is an array nonetheless. So after we call countup recursively the value of countArray will be an empty array. So as you can see, countArray is an array!
The logical jump here is that this also works when we call countup(2), or countup(3), or as high as you want to go. In the end, we will always reach the base case and get that empty array because each time we call countup recursively we are reducing the number by 1.