WEBVTT

00:01.230 --> 00:07.440
It's time to introduce Luthra Harper in the section I'm going to describe Reduce Harper, which actually

00:07.440 --> 00:11.190
is different and a bit complicated than previous a Harper's.

00:12.100 --> 00:17.950
In order to explain how Ridge's helper actually works, I'm going to start with a classical example,

00:17.950 --> 00:23.860
which you may come across if you try to find information about reduce at first, as usually I'm going

00:23.860 --> 00:26.190
to compare this helper to follow up.

00:26.950 --> 00:29.590
Suppose that we have a right with some numbers.

00:31.230 --> 00:35.370
For example, 100, 200 and 300.

00:37.210 --> 00:43.750
Then we want to sum those numbers and return the total sum as a single value for that, I'm going to

00:43.750 --> 00:50.230
create a variable sum, which actually will be initial value, and let's set it as zero.

00:51.630 --> 00:53.370
After that creates for a loop.

00:54.330 --> 01:01.050
White's verbal eye equals zero, then eyes less than no length.

01:03.590 --> 01:12.530
And the increment inside coal prices, right, verbal some because of that, we want to add numbers

01:12.530 --> 01:18.440
and then save value after each iteration we want to use plus equals operator.

01:19.840 --> 01:21.040
And then numbers.

01:24.780 --> 01:27.180
OK, let's run in Castle Deferrable some.

01:30.370 --> 01:35.920
Reload the page and see that we have 600, the total sum of items in a right.

01:36.760 --> 01:37.250
All right.

01:37.270 --> 01:41.320
It works fine, but no, I'm going to achieve the same using reduce.

01:42.220 --> 01:43.930
Let's make you a little border.

01:46.060 --> 01:49.000
And then create a new variable called a total.

01:50.770 --> 01:52.440
Assigned to it numbers.

01:53.370 --> 01:55.750
And use your reduce helper.

01:56.280 --> 02:02.970
Again, we have to pass your anonymous function, this function receives at least two parameters, generally

02:02.970 --> 02:03.510
JavaScript.

02:03.510 --> 02:08.390
The first parameter is called as previous or sometimes as an accumulator.

02:08.910 --> 02:10.170
You can use any of them.

02:10.680 --> 02:14.790
So this parameter actually accumulates values after each iteration.

02:15.720 --> 02:21.150
As for the second parameter, it's again current item as it was in case of previous.

02:21.150 --> 02:22.740
Helper's so right.

02:22.740 --> 02:23.280
No.

02:25.940 --> 02:33.350
Rudi's Harper may have more parameters like index of a right and the right self, but using those parameters

02:33.350 --> 02:34.680
are optional.

02:35.420 --> 02:43.950
Now let's return previous plus no actually reduce Harper itself receives two parameters.

02:44.390 --> 02:49.970
First, one is callback function, which we have already passed here, and second one is the value of

02:49.970 --> 02:51.210
the previous parameter.

02:51.860 --> 02:57.290
In other words, it's an initial value and in this case we need to set it as zero.

02:57.980 --> 03:00.080
Let's log total variable in council.

03:04.280 --> 03:08.740
Reload and we have the same result, which is 600.

03:09.410 --> 03:13.290
All right, as usual, let's see how it is, how it works.

03:14.180 --> 03:16.520
So here we have a right with numbers.

03:16.550 --> 03:24.890
And as you saw, we have set previous parameter or an initial value as zero on the first iteration of

03:24.890 --> 03:27.890
function is executed for the first item 100.

03:28.400 --> 03:32.600
The value of previous parameter is zero, which is added to 100.

03:34.570 --> 03:41.850
After the first iteration, the value of previous parameter becomes 100, then starts second iteration,

03:42.250 --> 03:48.450
the function is executed for the second item 200, which will be added to previous value 100.

03:49.180 --> 03:56.440
So after the second iteration will get three hundred, then start a third iteration, the function is

03:56.440 --> 03:58.680
executed for the third item of array.

03:59.230 --> 04:02.390
It's added to previous value, which is 300.

04:02.680 --> 04:07.080
And finally, as a result, we got 600 as a single value.

04:08.200 --> 04:08.890
Makes sense.

04:10.370 --> 04:10.830
All right.

04:11.380 --> 04:13.390
This is the way how reduced Hopper works.

04:13.750 --> 04:17.020
Let's go back to Brackett's and write some more real world examples.

04:17.950 --> 04:20.770
Suppose that person has fixed monthly salary.

04:20.770 --> 04:27.010
And also besides that, he has weekly bonuses, assumed that we want to get the total monthly income

04:27.010 --> 04:27.680
of that person.

04:28.240 --> 04:29.680
So create an array.

04:31.240 --> 04:38.200
And call it weekly earnings, let's insert in that area some numbers.

04:43.140 --> 04:46.300
Then create another variable and call it income.

04:47.760 --> 04:56.520
We're going to use again to reduce Holper, so let's assign to that variable weekly earnings that reduce.

04:57.910 --> 05:05.440
Then anonymous function, this function will receive two parameters for previous value, let's insert

05:05.440 --> 05:11.050
here previous and as a current item, write weekly earning.

05:14.070 --> 05:21.710
In order to get this some of those earnings, we have to return previous plus weekly earnings.

05:24.020 --> 05:29.580
And as we said, person has fixed monthly salary, which will be added to weekly earnings.

05:29.990 --> 05:32.360
We're going to set it as initial value.

05:32.750 --> 05:41.750
So right as the second parameter for reduce helper, for example, 5000, let's log in council variable

05:41.750 --> 05:42.290
income.

05:46.380 --> 05:46.980
Revolt.

05:49.150 --> 05:55.420
And we have here the total monthly income of person, which consists of weekly earnings and also a fixed

05:55.420 --> 05:55.930
salary.

05:57.540 --> 06:03.660
All right, I believe you guess how useful and convenient reduce Harper should be, but you also may

06:03.660 --> 06:06.970
think that we can use to reduce Harper only with numbers.

06:07.470 --> 06:08.700
Actually, it's not right.

06:09.180 --> 06:12.240
And in order to prove that, I'm going to write another example.

06:13.290 --> 06:17.370
Suppose that we have a race for first names and four last names.

06:17.610 --> 06:19.650
So create variable first names.

06:21.240 --> 06:35.460
And insert here some names, John, then Jane and Mary also create another variable for last names and

06:35.460 --> 06:40.010
place here, the items, Smith, Brown and dope.

06:41.660 --> 06:48.680
And suppose that we want to create one new array, which will include full names of the persons like

06:48.680 --> 06:55.640
John Smith, Jane Brown and Mary Jo, so create a new variable and name it as full names.

06:58.490 --> 06:59.630
Then assigned to it.

07:00.670 --> 07:04.570
First names and use here, reduce helper.

07:06.510 --> 07:08.280
Place, anonymous function.

07:09.250 --> 07:11.200
With two parameters.

07:12.180 --> 07:13.320
Previous and.

07:14.900 --> 07:17.060
Then as current item, right, first name.

07:20.910 --> 07:27.390
So, as we said, full names should be a new arraying, therefore reduce Holper will have as a second

07:27.390 --> 07:32.540
parameter and actually as an initial previous value and empty a right.

07:32.790 --> 07:38.370
And in order to accumulate first names and last names in that array, we're going to use push method.

07:38.970 --> 07:39.270
So.

07:39.270 --> 07:39.550
Right.

07:39.570 --> 07:41.850
Previous that push.

07:43.370 --> 07:51.830
That inside parenthesis, right, first name plus the space, and now we need to get proper last names

07:52.280 --> 07:58.850
for that, we have to use second array last names, but actually it's a right and it doesn't represent

07:59.240 --> 08:01.200
current item like first name.

08:01.640 --> 08:07.880
So in order to get last names, we need to iterate through this array and get the relevant items for

08:07.880 --> 08:08.060
that.

08:08.060 --> 08:09.830
We are going to use little trick.

08:10.310 --> 08:14.330
As we said previously, reduce Harper can receive more than two parameters.

08:14.900 --> 08:20.950
One of them is indexed, which generally should represent the index number of first names array.

08:21.710 --> 08:24.710
But because of that, both arrays have the same length.

08:25.310 --> 08:30.740
Three I'm going to use in this parameter for last names, array items.

08:31.660 --> 08:39.700
But again, remember that generally in this parameter represents in the case of Iraq, for which reduce

08:39.700 --> 08:43.180
Harper is used in this case, the array of first names.

08:44.080 --> 08:44.530
All right.

08:44.860 --> 08:47.400
So let's go ahead and write last names.

08:49.960 --> 08:58.030
And in square brackets index and the last thing to do is to return previous value.

09:00.230 --> 09:03.490
Which actually will give us a final full names.

09:03.530 --> 09:03.860
All right.

09:05.910 --> 09:07.160
OK, let's run it.

09:07.160 --> 09:08.780
Council the variable names.

09:11.220 --> 09:12.150
Reloader page.

09:13.250 --> 09:18.950
And you see that we have a new era which consists of full names, John Smith, James Brown and Mary

09:18.950 --> 09:22.280
Jo, the result that we wanted to get.

09:22.870 --> 09:23.380
All right.

09:23.900 --> 09:29.300
I've written this example just for demonstration that reduced Harper can be used not only with numbers,

09:29.750 --> 09:31.250
but with other data as well.

09:32.290 --> 09:34.980
OK, this was all about release, Harper.

09:35.030 --> 09:39.590
It's a little bit more complicated than previous Harper's, but I hope you could understand it well.

09:40.340 --> 09:40.790
All right.

09:40.940 --> 09:41.590
Let's move on.
