All right, so continuing with our exercise, we just have one left, so let's check it out, see the
shopping list below.
So before reading the rest, I'm just going to copy this and paste it inside the code.
So this is number four.
And now we have the shopping list, so now let's go back there to see what we need to do, so do the
following changes to the list, get the last item and place it at the beginning of the list.
Then we just need to add two more items at the end of the list and show the updated list.
Let's just copy this.
Take it to the code just so we can see it.
So now we just need to get the last element of the list and place it at the beginning, but I cannot
count how many elements I have and just use a manual index because the code that we write must work
with any list.
So if we have a list of three elements, the same code is going to work.
If we have a list of 30 elements, the same code is going to work.
So how do we get the last element dynamically?
Well, to get the last element of any array, there's a trick we can do that uses the length of the
array, so.
Let's just create a variable called last.
Element.
And now we're going to get this array.
And we need to pass something as the index, so any time you need to get the last element, you can
just use the length of the array.
I'm going to explain this in a moment.
So as an index, we're going to use the length of the array.
And then we can just subtract one, how does this work?
So let's forget this.
Let's create.
Another array with only two aliment, so we can see how does this work?
So here we have two elements and what are the indexes of those elements?
So this one is index zero and this one is index one, right?
If we do length of this array, what's the number that we are going to get, we are going to get to
because it has to element.
So this means that the last element of an array is always its length, minus one.
So its length is two.
Minus one is exactly the index of the last element.
So by doing this, it doesn't matter if we have an array of 30 elements, three hundred elements, three
element, it is always going to get the last element.
So by doing this, we already saved the last element.
And now we can just do.
Shopping list.
That pop.
We know that this is going to remove the last element, and now we already have it.
And then we can do shopping list dot.
On shift, which is the method to add elements to the beginning of an array.
So then we can just use this element that we got before we removed it.
So now before moving ahead, let's just do consult that log.
With the shopping list, just to see if we were able to get chocolate and place it at the beginning
of the array.
Refreshing the page now we can see the chocolate is now the first item.
And like I said, it's important to do it the way we did now, because now it doesn't matter the size
of our array, this operation that we just created is always going to work.
All right.
So now let's move on to the next add two more items at the end of the list.
So this should be pretty simple to do.
We can just use shopping list.
That Bush.
Which is the method to add these elements to the end.
We just need to do this separately because these need to be.
Separate elements, we don't want cheese and eggs together, so cheese.
Eggs.
Going back there, refreshing the page, now we can see that we moved chocolate to the beginning and
we added cheese and eggs to the end of the array.
So now we are done with this exercise before I finish this video, I just want to show you something
interesting.
There's an easier way to do this chocolate operation that we did.
Let's just make one test here.
So we have this variable called shopping list.
We can access it from the console.
As we can see, we have 10 elements.
And what happens when we use the.
Method, so I'm just going to do pop.
I'm going to press enter.
And as you can see.
This method not only removes the element, but it also returns something, so it's returning eggs to
us.
So this means that we don't need to get the length of this array and do all we did just to get the last
element, because this method not only is going to remove this element, it is also going to return
it.
So going back to our code or let's try it here first if we do a shopping list.
Then we use the on shift method.
To add elements to the beginning of this array.
What we can do is just getting this pop.
Method.
So when we executed inside here, not only it's going to remove the last element, but it is always
going to return which element was removed.
So then we can use this element in the on shift method.
So now we just remove removed eggs.
Before doing this, let's check the array again.
So now this is how the shopping list looks like the last element is cheese, so if we execute this.
First, it is going to remove the cheese, then it is going to return the item that has been removed.
So it is going to replace.
She's here.
So by doing this, we are able to add it to the beginning of the array.
Let's see if this works.
Let's just press enter.
Now, let's check the shopping list.
And we can see that we just did the same thing with less lines of code, so let's go back there and
let's add this different solution.
So here we have the solution for the Shacklett, which used three lines of code.
I'm just going to comment this shift at a.
And now the only thing we need to do here.
It's just using the UN shift.
And here we can just use shopping lists, dot, pop, because this not only is going to remove chocolate,
is also going to return what has been removed and by returning it, we can use it inside the unshaved
method.
So saving this.
Refreshing the page, we can see that we have the same result.
So I hope does not overcomplicate thanks to you.
I just wanted to show you this, because when we are writing code, we should always try to be efficient,
which means using less lines of code to get to the same result.
So that was it for this exercise.
In the next video, we are going to start diving deeper into JavaScript.
I'll see you then.