WEBVTT 00:00:06.810 --> 00:00:12.310 In this video we're going to learn the functions which will help us manipulates a race. 00:00:12.600 --> 00:00:20.640 The first function we're going to be looking at is a function which will merge two or more race into 00:00:20.640 --> 00:00:22.100 one array. 00:00:22.290 --> 00:00:27.080 So let's create a couple of arrays. 00:00:27.750 --> 00:00:31.110 First went shopping baskets one. 00:00:32.360 --> 00:00:33.800 OK. 00:00:34.330 --> 00:00:41.030 And let's place values inside this numeric array. 00:00:41.430 --> 00:00:43.470 Bread for example. 00:00:43.470 --> 00:00:52.930 Then milk then eggs and it creates a second one. 00:00:53.490 --> 00:00:56.550 So we must get to also in America. 00:00:56.940 --> 00:01:04.670 At least half apple banana and orange. 00:01:05.040 --> 00:01:13.000 So now we want to merge these arrays so we're going to create a third a shopping basket. 00:01:13.130 --> 00:01:13.830 OK. 00:01:13.860 --> 00:01:18.240 And we're going to use the function array underscore merge. 00:01:18.630 --> 00:01:26.670 So this function is going to take us promises or arguments two or more arrays and is going to return 00:01:26.700 --> 00:01:32.580 the resulting array which we are going to store inside our variable shopping basket. 00:01:32.580 --> 00:01:33.410 OK. 00:01:33.720 --> 00:01:41.470 So let's place the couple of arrays inside the promises of this function. 00:01:42.870 --> 00:01:51.670 Ok so now let's print the resulting array which is shopping baskets. 00:01:51.800 --> 00:01:54.600 Well let's have a paragraph first saying 00:01:57.130 --> 00:02:03.390 a shopping basket. 00:02:04.980 --> 00:02:05.680 OK. 00:02:05.940 --> 00:02:15.530 And let's preens the array using preens underscore our function. 00:02:17.330 --> 00:02:23.350 OK so let's refresh the page. 00:02:27.540 --> 00:02:30.730 Let's read it again and again. 00:02:31.050 --> 00:02:31.350 OK. 00:02:31.380 --> 00:02:40.320 Now we can see the results so we can see that the new array is a combination of the first and the second 00:02:40.320 --> 00:02:41.530 array. 00:02:41.550 --> 00:02:42.880 All right. 00:02:43.590 --> 00:02:51.200 OK so now let's count the number of items inside the resulting array. 00:02:51.270 --> 00:02:52.260 OK. 00:02:52.680 --> 00:02:55.400 So I'm going to echo 00:02:59.220 --> 00:02:59.790 number 00:03:02.700 --> 00:03:08.640 of items in shopping baskets. 00:03:10.240 --> 00:03:20.670 OK this time we're going to use a function called counts which will take as a parameter an array and 00:03:20.670 --> 00:03:25.580 return the number of values or number of items inside that array. 00:03:25.770 --> 00:03:32.680 OK so if we save and refresh we can see that's our array has got 6 elements inside. 00:03:33.020 --> 00:03:42.470 OK if we add another element to the array so let's give it to the first basket 00:03:45.140 --> 00:03:47.330 and add another bread. 00:03:48.210 --> 00:03:59.880 OK so now we can see the big array has got seven elements in it. 00:04:00.600 --> 00:04:03.350 OK. 00:04:03.370 --> 00:04:05.100 All right. 00:04:06.060 --> 00:04:10.440 Now we going to see how to count each value in an array. 00:04:10.470 --> 00:04:15.920 So we want to know how many breads we have how many milk how many eggs and so on. 00:04:16.140 --> 00:04:16.930 OK. 00:04:17.490 --> 00:04:24.970 So for each value we are going to count the occurrence occurrences of that's a value. 00:04:25.110 --> 00:04:31.670 So to do that we're going to create a new variable. 00:04:31.670 --> 00:04:38.480 We're going to quality counts and then we're going to use a function called array. 00:04:39.390 --> 00:04:42.920 And the score counts and the score values. 00:04:42.990 --> 00:04:47.870 So this function is going to take as a barometer an array. 00:04:47.950 --> 00:04:49.630 I'm going to give it shopping basket. 00:04:50.010 --> 00:04:53.290 And he's going to return another array. 00:04:53.430 --> 00:04:58.110 OK we're going to see what's that array looks like. 00:04:58.110 --> 00:05:01.620 So what we're going to do we are going to echo a paragraph. 00:05:02.160 --> 00:05:03.760 OK. 00:05:05.210 --> 00:05:16.500 And we going to write basket's counts and now we are going to print this variable counts using prints 00:05:16.550 --> 00:05:19.350 are because it's an array. 00:05:21.170 --> 00:05:21.940 OK. 00:05:22.680 --> 00:05:32.060 So now if we refresh the page we can see what the variable count looks like. 00:05:32.060 --> 00:05:33.410 So it's in there right. 00:05:33.420 --> 00:05:34.500 So. 00:05:34.560 --> 00:05:39.160 So the keys are the values of the original array. 00:05:39.480 --> 00:05:40.070 OK. 00:05:40.380 --> 00:05:47.520 And the values are the counts of those bodies. 00:05:47.520 --> 00:05:52.510 OK so you've got two reds one milk one eggs and so on. 00:05:52.960 --> 00:05:57.330 OK so now let's see how many 00:06:00.240 --> 00:06:01.380 breads we have. 00:06:01.380 --> 00:06:06.670 So we just need to access this array and use the key bread. 00:06:06.690 --> 00:06:07.910 So if we Ekho 00:06:13.170 --> 00:06:13.710 number 00:06:16.560 --> 00:06:20.720 of bread items in baskets 00:06:24.330 --> 00:06:24.600 OK 00:06:28.890 --> 00:06:31.560 can just add to that's counts. 00:06:31.680 --> 00:06:36.530 And then we access this value using the key. 00:06:36.550 --> 00:06:41.730 Brett OK. 00:06:42.000 --> 00:06:51.170 So if you refresh we can see that if you got two bread items inside the baskets. 00:06:51.180 --> 00:06:55.320 All right now we can use another function 00:06:57.990 --> 00:07:04.760 which will tell us whether a certain value is inside an array or not. 00:07:04.800 --> 00:07:10.920 So we're just going to check for example if our shopping basket contains any bread. 00:07:10.970 --> 00:07:17.090 So so we're going to check that condition and then if it's true going to echo something. 00:07:18.000 --> 00:07:20.000 OK. 00:07:20.130 --> 00:07:25.880 Otherwise we are going to echo something else. 00:07:27.350 --> 00:07:28.290 OK. 00:07:28.530 --> 00:07:29.130 So 00:07:32.550 --> 00:07:39.580 the function we're going to use is called In called array that is going to take a couple of promises 00:07:39.580 --> 00:07:39.640 . 00:07:39.640 --> 00:07:44.850 So the first parameter is the value we checking if it's if it's there. 00:07:45.120 --> 00:07:48.860 And the second prong which is the rate self which is soapie muskets. 00:07:49.290 --> 00:07:50.330 OK. 00:07:50.640 --> 00:07:53.690 So is going to return a boolean. 00:07:53.790 --> 00:07:55.310 True or false. 00:07:55.320 --> 00:08:01.120 So it is true which means we will call it a bread item inside the array. 00:08:01.290 --> 00:08:02.880 Then we get an echo. 00:08:02.880 --> 00:08:06.970 There is bread in the basket. 00:08:07.620 --> 00:08:08.060 OK. 00:08:08.070 --> 00:08:16.590 Otherwise we are going to echo a paragraph saying there is no bread in the box. 00:08:16.650 --> 00:08:25.170 It's OK if we refresh the page we can see that there is bread in the baskets. 00:08:25.800 --> 00:08:28.140 OK. 00:08:28.140 --> 00:08:32.780 All right so now we're going to try another function. 00:08:32.780 --> 00:08:36.750 So this time we're going to 00:08:41.100 --> 00:08:44.340 add another item to the array. 00:08:44.640 --> 00:08:45.740 OK. 00:08:46.290 --> 00:08:51.830 So let's say we want to add yogurt to our shopping baskets. 00:08:52.170 --> 00:08:56.760 So we're going to use the function array at this push. 00:08:57.300 --> 00:08:57.870 OK. 00:08:58.110 --> 00:09:01.230 So it's going to take two arguments. 00:09:01.230 --> 00:09:08.640 So the first one is the array and the second argument is whatever we want to push inside the array. 00:09:09.260 --> 00:09:11.910 We want to have yogurt. 00:09:12.000 --> 00:09:19.660 So what the function is going to do is just going to change the value of the original array. 00:09:19.710 --> 00:09:23.880 Now the the array is going to have 00:09:26.490 --> 00:09:27.970 this item at the end. 00:09:28.200 --> 00:09:28.670 OK. 00:09:28.980 --> 00:09:30.130 So that's right. 00:09:30.350 --> 00:09:33.270 Let's echo a paragraph 00:09:35.880 --> 00:09:36.450 saying 00:09:39.990 --> 00:09:40.470 there is 00:09:44.100 --> 00:09:50.950 shopping baskets after adding yogurt. 00:09:51.940 --> 00:09:59.100 OK let's just print the shopping malls because again she's going to copy this 00:10:01.950 --> 00:10:06.020 and now you can see that you. 00:10:06.040 --> 00:10:09.200 Cause you're great at the end. 00:10:09.360 --> 00:10:10.770 All right. 00:10:12.120 --> 00:10:14.900 OK. 00:10:17.280 --> 00:10:27.530 Now we're going to create a form where the user is going to insert an additional item. 00:10:27.750 --> 00:10:28.680 OK. 00:10:29.250 --> 00:10:37.010 And if you've got an item submitted by the user then we're going to have that item of yogurt. 00:10:37.250 --> 00:10:38.860 OK. 00:10:38.910 --> 00:10:41.880 So let's create the form first. 00:10:42.070 --> 00:10:46.170 So it's going to be a simple simple form with 00:10:49.050 --> 00:10:54.260 a text input. 00:10:55.080 --> 00:11:06.710 So we're going to give you that type text a name item and then the item going to have a submit button 00:11:06.730 --> 00:11:06.770 . 00:11:06.770 --> 00:11:13.960 So it's going to be input of type submit and name some it's 00:11:17.270 --> 00:11:34.880 value something and it's on the label to the text inputs and say Item 2 shopping baskets. 00:11:35.310 --> 00:11:40.480 OK let's give it's four attributes going to light. 00:11:41.400 --> 00:11:45.270 OK so now we're going to do again. 00:11:46.590 --> 00:11:56.700 So before printing the offset printing the shopping basket of the yogurt we are going to check if the 00:11:56.700 --> 00:11:58.180 form is submitted. 00:11:58.440 --> 00:12:05.470 So we're going to go to the get ready and are going to check if we have a submit. 00:12:06.800 --> 00:12:07.480 OK. 00:12:07.710 --> 00:12:14.090 If yes then we are going to check if the user submitted an item. 00:12:14.100 --> 00:12:20.990 So you going to check if we have gets item OK. 00:12:21.210 --> 00:12:33.330 And if yes then we are going to add that item to the array using a rate wish rather than yogurt. 00:12:33.350 --> 00:12:38.200 We are going to use the submitted value. 00:12:38.400 --> 00:12:39.360 Okay. 00:12:39.700 --> 00:12:48.150 And let's not forget to make sure that's the form has cause a method gets. 00:12:48.150 --> 00:12:48.510 OK. 00:12:48.780 --> 00:12:51.370 So let's refresh the page. 00:12:51.410 --> 00:13:04.230 So now if we are an item to add to that form so let's add for example pineapple if we submitted. 00:13:06.330 --> 00:13:06.570 Okay. 00:13:06.570 --> 00:13:08.130 So let's make sure that we can 00:13:14.420 --> 00:13:22.340 go yes we need to bring the shopping baskets again it's printed 00:13:27.930 --> 00:13:28.430 this 00:13:33.950 --> 00:13:39.950 refreshed the page and now we can see that will cause an extra element the pineapple. 00:13:40.040 --> 00:13:40.470 OK. 00:13:40.770 --> 00:13:44.240 So if we see some meats and other elements. 00:13:44.580 --> 00:13:45.490 So if we submit 00:13:48.510 --> 00:13:54.020 no elements no ice item then we see that there is no extra element there. 00:13:54.210 --> 00:14:07.200 But if we submit anything else for example Bill Gates submits we can see that the item is added to the 00:14:07.670 --> 00:14:08.510 array. 00:14:09.120 --> 00:14:10.070 Okay