It's time to introduce Luthra Harper in the section I'm going to describe Reduce Harper, which actually
is different and a bit complicated than previous a Harper's.
In order to explain how Ridge's helper actually works, I'm going to start with a classical example,
which you may come across if you try to find information about reduce at first, as usually I'm going
to compare this helper to follow up.
Suppose that we have a right with some numbers.
For example, 100, 200 and 300.
Then we want to sum those numbers and return the total sum as a single value for that, I'm going to
create a variable sum, which actually will be initial value, and let's set it as zero.
After that creates for a loop.
White's verbal eye equals zero, then eyes less than no length.
And the increment inside coal prices, right, verbal some because of that, we want to add numbers
and then save value after each iteration we want to use plus equals operator.
And then numbers.
OK, let's run in Castle Deferrable some.
Reload the page and see that we have 600, the total sum of items in a right.
All right.
It works fine, but no, I'm going to achieve the same using reduce.
Let's make you a little border.
And then create a new variable called a total.
Assigned to it numbers.
And use your reduce helper.
Again, we have to pass your anonymous function, this function receives at least two parameters, generally
JavaScript.
The first parameter is called as previous or sometimes as an accumulator.
You can use any of them.
So this parameter actually accumulates values after each iteration.
As for the second parameter, it's again current item as it was in case of previous.
Helper's so right.
No.
Rudi's Harper may have more parameters like index of a right and the right self, but using those parameters
are optional.
Now let's return previous plus no actually reduce Harper itself receives two parameters.
First, one is callback function, which we have already passed here, and second one is the value of
the previous parameter.
In other words, it's an initial value and in this case we need to set it as zero.
Let's log total variable in council.
Reload and we have the same result, which is 600.
All right, as usual, let's see how it is, how it works.
So here we have a right with numbers.
And as you saw, we have set previous parameter or an initial value as zero on the first iteration of
function is executed for the first item 100.
The value of previous parameter is zero, which is added to 100.
After the first iteration, the value of previous parameter becomes 100, then starts second iteration,
the function is executed for the second item 200, which will be added to previous value 100.
So after the second iteration will get three hundred, then start a third iteration, the function is
executed for the third item of array.
It's added to previous value, which is 300.
And finally, as a result, we got 600 as a single value.
Makes sense.
All right.
This is the way how reduced Hopper works.
Let's go back to Brackett's and write some more real world examples.
Suppose that person has fixed monthly salary.
And also besides that, he has weekly bonuses, assumed that we want to get the total monthly income
of that person.
So create an array.
And call it weekly earnings, let's insert in that area some numbers.
Then create another variable and call it income.
We're going to use again to reduce Holper, so let's assign to that variable weekly earnings that reduce.
Then anonymous function, this function will receive two parameters for previous value, let's insert
here previous and as a current item, write weekly earning.
In order to get this some of those earnings, we have to return previous plus weekly earnings.
And as we said, person has fixed monthly salary, which will be added to weekly earnings.
We're going to set it as initial value.
So right as the second parameter for reduce helper, for example, 5000, let's log in council variable
income.
Revolt.
And we have here the total monthly income of person, which consists of weekly earnings and also a fixed
salary.
All right, I believe you guess how useful and convenient reduce Harper should be, but you also may
think that we can use to reduce Harper only with numbers.
Actually, it's not right.
And in order to prove that, I'm going to write another example.
Suppose that we have a race for first names and four last names.
So create variable first names.
And insert here some names, John, then Jane and Mary also create another variable for last names and
place here, the items, Smith, Brown and dope.
And suppose that we want to create one new array, which will include full names of the persons like
John Smith, Jane Brown and Mary Jo, so create a new variable and name it as full names.
Then assigned to it.
First names and use here, reduce helper.
Place, anonymous function.
With two parameters.
Previous and.
Then as current item, right, first name.
So, as we said, full names should be a new arraying, therefore reduce Holper will have as a second
parameter and actually as an initial previous value and empty a right.
And in order to accumulate first names and last names in that array, we're going to use push method.
So.
Right.
Previous that push.
That inside parenthesis, right, first name plus the space, and now we need to get proper last names
for that, we have to use second array last names, but actually it's a right and it doesn't represent
current item like first name.
So in order to get last names, we need to iterate through this array and get the relevant items for
that.
We are going to use little trick.
As we said previously, reduce Harper can receive more than two parameters.
One of them is indexed, which generally should represent the index number of first names array.
But because of that, both arrays have the same length.
Three I'm going to use in this parameter for last names, array items.
But again, remember that generally in this parameter represents in the case of Iraq, for which reduce
Harper is used in this case, the array of first names.
All right.
So let's go ahead and write last names.
And in square brackets index and the last thing to do is to return previous value.
Which actually will give us a final full names.
All right.
OK, let's run it.
Council the variable names.
Reloader page.
And you see that we have a new era which consists of full names, John Smith, James Brown and Mary
Jo, the result that we wanted to get.
All right.
I've written this example just for demonstration that reduced Harper can be used not only with numbers,
but with other data as well.
OK, this was all about release, Harper.
It's a little bit more complicated than previous Harper's, but I hope you could understand it well.
All right.
Let's move on.