Now after watching some youtube videos and reading up google results, I have a slightly better understanding of whats happening. The function does something, and then it calls itself and repeats the function and would otherwise be infinite, except that the base case helps to prevent it from doing so. In one example i found on youtube, it went like this:
Now in THIS example, I can understand whats happening. The base case will eventually reach one and the function stops. But there’s another thing: I can clearly see that sayHi is being called again in the else statement just how someone may call a function normally.
But in the project that FreeCodeCamp provided, it calls the function in a different way. It calls it with const countArray = countup(n - 1); And thats super weird to me because I’m not sure how that’s working. Is it common to call a function by declaring it as a value to a new variable? And does it function differently? Is it being called in a different way or under different mechanics than just calling it normally? What exactly is happening here and why was it called this way
I’m not exactly sure because when the base case is reached, it does return an array. But just looking at the code, since the base case isn’t reached (the base case is false) it shouldn’t return an array yet.
But if i run it with the number 1, it returns an array with 1.
edit: nope i see it now, it runs the 2nd part and pushes that into the array because the base case isn’t reached until it hits zero, whoops
Ok let me try. If you run countup(1) the base case fails and it creates a new variable called countarray which is countArray
Then the 2nd instruction pushes the 1 into the array so its:
Then it returns countArray. Idk what that line means, “return countArray”. Its not a function. So yea idk what happens at this particular line, what does return countArray do
BTW, whenever you want to refer to code in here, wrap the code in single back ticks if you want to do it inline or triple back ticks if you want to make it a code block. Or you can use the </> button on the editor tool bar.
Is a return statement which immediately ends the function and returns the value in the return statement. So in this case it causes the function to return the value in countArray, which is an array.
In the sum function I mentioned earlier, it would have a return statement of something like:
return firstNum + secondNum;
The return statement is how the function gives us the value we are expecting when we call the funciton. It wouldn’t be much help if we called sum(1048038140198, 02139402024350) to get the sum of those two numbers but the function didn’t return the sum to us
Correct, it pushes 1 into the empty array held by countArray. And then the next line:
Returns the value  since that is what is stored in countArray.
So you have just figured out what countup(1) returns. Now explain what countup(2) returns. Again, go through it line by line just like we did with countup(1). No shortcuts. And remember, you already know what countup(1) returns.
It looks to me like its creating a new local variable, an array, called countArray. And then its pushing 2 (or whatever n is) into the array, creating that array. During all the iterations of the function, this object seems to be saving the numbers one by one, so it feels like it exists as a local object inside the function