EDIT: My initial explanation and info in this reply was dubious at best and needed editing and hopefully has been fixed to explain it correctly
Please see this reply and this reply for further clarity.
I got that backwards by mistake and have corrected it in the original reply.
Generally it is not about increasing or decreasing order. It is about what is the insertion point where items are being placed into the array.
Well, I should say, we do care about the order in which the recursive part of the function is going to return the values it calculates to the method, because that will dictate what method we need to choose to accomplish this.
unshift() method inserts them each at
array (the start of the array).
push() method inserts them each at
array[array.length - 1] (the end of the array).
We are in the context of a very specific recursion function, so
unshift()will give us the correct order, and
push() will reverse the order.
edit: [ The recursion calculations happen first starting from 5, but these calculated values of n are stored in what’s called a stack. The values are pulled off the stack when the recursion is done, in First In Last Out order… so 1, 2, 3, 4, 5 in this case. So you need to use the correct method to account for that and end up with an array that meets the requirements of the challenge. ]
The challenge is introducing us to the above methods, but is also introducing us to and demonstrating recursion…
Lets say we want to write a function that will work for any given array length. We won’t necessarily know the length before hand. We just know that the index of the last item in that array will be arr.length - 1, so let’s refer to the unknown length,
So to get all the elements in an array of
n length, how could we write a recursive function that works in a general case of an array of
Remember, the recursion is “counting down” from 5 to to end up at 0.
Using the info we know for an array of
n length, we know we are done when we hit 0 because we’ve gone through all of the possible items that can be stored in the array.
You can have an array of length 0, but an array of length zero minus one, or “negative 1”, doesn’t really make much sense.
The value of the length of an array is always going to be the index number of the item at the end of the array plus one (
array[last_item_index] + 1 ) because arrays are 0 indexed.
The index of the last item in the array is always going to be the value of the length of the array minus 1 (
array.length - 1 ), again because arrays are 0 indexed.
Both are saying the same thing, I know. But it’s a matter of context and perspective and if you mix this up, you make errors like I made and had to fix in my initial replies… off-by-one errors or mixing up order of things if you’re dealing with multiple things like recursion,
unshift(), etc., all at the same-ish time (people do it all the time, as I had done earlier).
So because of what we know from above, we know that our base case test to end the recursion needs to be when
n -1 = 0, or when
n = 1 …
(1 - 1 = 0)
We will test for the base case condition in our function first. If that condition hasn’t been met, we want the recursion to keep going until the condition is met.
If we call our function with a parameter of 5 for an array of length 5 then we’d expect our function recursion to go like this :
BEGINNING OF EDIT
This section of the reply has been edited to correct some errors and oversights
n = 5, ( n -1 ) = ( 5 - 1 ) = 4
n = 4, ( n - 1 ) = (4 - 1 ) = 3
n = 3, ( n - 1 ) = (3 - 1 ) = 2
n = 2, ( n - 1 ) = (2 - 1 ) = 1
n = 1, ( n - 1 ) = (1 - 1 ) = 0 <— Base Case, recursion stops
we should expect to get all the indexes starting from 4 to 0 (4, 3, 2, 1, 0) and we could then use those indexes to get the values associated with those indexes.
arr, arr, arr, arr, arr.
The paragraph above is crossed out because it incorrect, or at least out of context and not applicable here. The recursion is not calculating array indexes, it is generating the array values.
I don’t know what I was, or if I was, even thinking there.
It’s like all of the sudden (due to lack of sleep, coffee, or whatever) my mind totally and randomly drops the idea of recursion all together and I begin to speak as if we are just pulling items out of an already created array and the recursion and
unshift() never happened.
My apologies for that.
END OF EDIT
You’re focusing on
push(). The point is to understand recursion and how it can be useful when we want to write a function in a generalized way but we don’t know what the given parameter might be.