1 00:00:09,180 --> 00:00:15,810 All right, so continuing with our exercise, we just have one left, so let's check it out, see the 2 00:00:15,810 --> 00:00:17,540 shopping list below. 3 00:00:17,580 --> 00:00:23,760 So before reading the rest, I'm just going to copy this and paste it inside the code. 4 00:00:25,620 --> 00:00:28,410 So this is number four. 5 00:00:35,370 --> 00:00:41,370 And now we have the shopping list, so now let's go back there to see what we need to do, so do the 6 00:00:41,370 --> 00:00:47,400 following changes to the list, get the last item and place it at the beginning of the list. 7 00:00:48,270 --> 00:00:54,450 Then we just need to add two more items at the end of the list and show the updated list. 8 00:00:54,850 --> 00:00:56,430 Let's just copy this. 9 00:00:57,540 --> 00:01:00,560 Take it to the code just so we can see it. 10 00:01:08,410 --> 00:01:15,460 So now we just need to get the last element of the list and place it at the beginning, but I cannot 11 00:01:15,460 --> 00:01:25,840 count how many elements I have and just use a manual index because the code that we write must work 12 00:01:25,840 --> 00:01:27,300 with any list. 13 00:01:27,550 --> 00:01:32,290 So if we have a list of three elements, the same code is going to work. 14 00:01:32,530 --> 00:01:36,910 If we have a list of 30 elements, the same code is going to work. 15 00:01:37,330 --> 00:01:40,930 So how do we get the last element dynamically? 16 00:01:42,470 --> 00:01:50,900 Well, to get the last element of any array, there's a trick we can do that uses the length of the 17 00:01:50,900 --> 00:01:52,190 array, so. 18 00:01:53,260 --> 00:01:55,570 Let's just create a variable called last. 19 00:01:58,400 --> 00:01:59,080 Element. 20 00:01:59,990 --> 00:02:02,270 And now we're going to get this array. 21 00:02:06,250 --> 00:02:13,570 And we need to pass something as the index, so any time you need to get the last element, you can 22 00:02:13,570 --> 00:02:15,760 just use the length of the array. 23 00:02:16,720 --> 00:02:19,090 I'm going to explain this in a moment. 24 00:02:20,380 --> 00:02:24,100 So as an index, we're going to use the length of the array. 25 00:02:26,480 --> 00:02:32,170 And then we can just subtract one, how does this work? 26 00:02:32,570 --> 00:02:33,980 So let's forget this. 27 00:02:37,110 --> 00:02:38,190 Let's create. 28 00:02:39,150 --> 00:02:44,780 Another array with only two aliment, so we can see how does this work? 29 00:02:45,880 --> 00:02:51,550 So here we have two elements and what are the indexes of those elements? 30 00:02:51,670 --> 00:02:57,160 So this one is index zero and this one is index one, right? 31 00:02:58,180 --> 00:03:04,930 If we do length of this array, what's the number that we are going to get, we are going to get to 32 00:03:04,930 --> 00:03:06,920 because it has to element. 33 00:03:07,330 --> 00:03:13,960 So this means that the last element of an array is always its length, minus one. 34 00:03:14,530 --> 00:03:16,420 So its length is two. 35 00:03:17,020 --> 00:03:21,130 Minus one is exactly the index of the last element. 36 00:03:21,460 --> 00:03:28,000 So by doing this, it doesn't matter if we have an array of 30 elements, three hundred elements, three 37 00:03:28,000 --> 00:03:32,050 element, it is always going to get the last element. 38 00:03:33,880 --> 00:03:38,710 So by doing this, we already saved the last element. 39 00:03:40,450 --> 00:03:42,490 And now we can just do. 40 00:03:44,390 --> 00:03:45,620 Shopping list. 41 00:03:47,310 --> 00:03:49,260 That pop. 42 00:03:53,320 --> 00:03:58,480 We know that this is going to remove the last element, and now we already have it. 43 00:04:00,900 --> 00:04:05,580 And then we can do shopping list dot. 44 00:04:06,640 --> 00:04:12,580 On shift, which is the method to add elements to the beginning of an array. 45 00:04:15,910 --> 00:04:21,400 So then we can just use this element that we got before we removed it. 46 00:04:22,210 --> 00:04:26,170 So now before moving ahead, let's just do consult that log. 47 00:04:28,090 --> 00:04:35,020 With the shopping list, just to see if we were able to get chocolate and place it at the beginning 48 00:04:35,020 --> 00:04:35,830 of the array. 49 00:04:42,020 --> 00:04:47,810 Refreshing the page now we can see the chocolate is now the first item. 50 00:04:48,980 --> 00:04:55,310 And like I said, it's important to do it the way we did now, because now it doesn't matter the size 51 00:04:55,310 --> 00:05:01,100 of our array, this operation that we just created is always going to work. 52 00:05:01,790 --> 00:05:02,300 All right. 53 00:05:02,300 --> 00:05:08,090 So now let's move on to the next add two more items at the end of the list. 54 00:05:08,090 --> 00:05:10,390 So this should be pretty simple to do. 55 00:05:11,210 --> 00:05:14,810 We can just use shopping list. 56 00:05:17,170 --> 00:05:18,370 That Bush. 57 00:05:20,180 --> 00:05:23,240 Which is the method to add these elements to the end. 58 00:05:23,620 --> 00:05:27,620 We just need to do this separately because these need to be. 59 00:05:28,610 --> 00:05:33,800 Separate elements, we don't want cheese and eggs together, so cheese. 60 00:05:40,370 --> 00:05:41,090 Eggs. 61 00:05:45,130 --> 00:05:51,610 Going back there, refreshing the page, now we can see that we moved chocolate to the beginning and 62 00:05:51,610 --> 00:05:54,790 we added cheese and eggs to the end of the array. 63 00:05:57,150 --> 00:06:03,240 So now we are done with this exercise before I finish this video, I just want to show you something 64 00:06:03,240 --> 00:06:04,010 interesting. 65 00:06:04,290 --> 00:06:09,120 There's an easier way to do this chocolate operation that we did. 66 00:06:09,510 --> 00:06:11,280 Let's just make one test here. 67 00:06:11,610 --> 00:06:15,990 So we have this variable called shopping list. 68 00:06:16,350 --> 00:06:18,510 We can access it from the console. 69 00:06:18,510 --> 00:06:20,580 As we can see, we have 10 elements. 70 00:06:21,000 --> 00:06:24,960 And what happens when we use the. 71 00:06:28,510 --> 00:06:30,730 Method, so I'm just going to do pop. 72 00:06:31,540 --> 00:06:33,190 I'm going to press enter. 73 00:06:34,970 --> 00:06:36,290 And as you can see. 74 00:06:38,910 --> 00:06:47,190 This method not only removes the element, but it also returns something, so it's returning eggs to 75 00:06:47,190 --> 00:06:47,500 us. 76 00:06:48,000 --> 00:06:54,960 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 77 00:06:54,960 --> 00:07:02,610 element, because this method not only is going to remove this element, it is also going to return 78 00:07:02,610 --> 00:07:02,850 it. 79 00:07:03,300 --> 00:07:09,540 So going back to our code or let's try it here first if we do a shopping list. 80 00:07:14,810 --> 00:07:17,840 Then we use the on shift method. 81 00:07:19,350 --> 00:07:22,200 To add elements to the beginning of this array. 82 00:07:23,880 --> 00:07:27,840 What we can do is just getting this pop. 83 00:07:29,780 --> 00:07:30,420 Method. 84 00:07:30,650 --> 00:07:37,940 So when we executed inside here, not only it's going to remove the last element, but it is always 85 00:07:37,940 --> 00:07:41,750 going to return which element was removed. 86 00:07:42,020 --> 00:07:47,010 So then we can use this element in the on shift method. 87 00:07:47,390 --> 00:07:49,370 So now we just remove removed eggs. 88 00:07:50,480 --> 00:07:53,810 Before doing this, let's check the array again. 89 00:07:55,770 --> 00:08:03,450 So now this is how the shopping list looks like the last element is cheese, so if we execute this. 90 00:08:05,030 --> 00:08:13,280 First, it is going to remove the cheese, then it is going to return the item that has been removed. 91 00:08:13,490 --> 00:08:14,900 So it is going to replace. 92 00:08:16,600 --> 00:08:17,260 She's here. 93 00:08:19,470 --> 00:08:24,240 So by doing this, we are able to add it to the beginning of the array. 94 00:08:24,300 --> 00:08:25,650 Let's see if this works. 95 00:08:25,930 --> 00:08:27,300 Let's just press enter. 96 00:08:29,340 --> 00:08:31,580 Now, let's check the shopping list. 97 00:08:33,040 --> 00:08:41,230 And we can see that we just did the same thing with less lines of code, so let's go back there and 98 00:08:41,230 --> 00:08:43,730 let's add this different solution. 99 00:08:44,740 --> 00:08:51,370 So here we have the solution for the Shacklett, which used three lines of code. 100 00:08:51,520 --> 00:08:54,910 I'm just going to comment this shift at a. 101 00:08:58,150 --> 00:09:00,490 And now the only thing we need to do here. 102 00:09:03,350 --> 00:09:05,510 It's just using the UN shift. 103 00:09:06,470 --> 00:09:14,090 And here we can just use shopping lists, dot, pop, because this not only is going to remove chocolate, 104 00:09:14,420 --> 00:09:21,470 is also going to return what has been removed and by returning it, we can use it inside the unshaved 105 00:09:21,470 --> 00:09:22,070 method. 106 00:09:22,700 --> 00:09:23,870 So saving this. 107 00:09:25,570 --> 00:09:29,840 Refreshing the page, we can see that we have the same result. 108 00:09:30,370 --> 00:09:33,940 So I hope does not overcomplicate thanks to you. 109 00:09:34,120 --> 00:09:40,420 I just wanted to show you this, because when we are writing code, we should always try to be efficient, 110 00:09:40,570 --> 00:09:45,760 which means using less lines of code to get to the same result. 111 00:09:46,510 --> 00:09:48,850 So that was it for this exercise. 112 00:09:49,510 --> 00:09:54,110 In the next video, we are going to start diving deeper into JavaScript. 113 00:09:54,250 --> 00:09:55,120 I'll see you then.