WEBVTT

00:01.380 --> 00:04.240
Let's move on to the next hour, helpers, in this lecture.

00:04.260 --> 00:07.010
I want to introduce you every and some helpers.

00:07.470 --> 00:12.980
I decided to demonstrate those helpers in one video because they work in a similar way as it was in

00:12.990 --> 00:14.790
case of filter and find helpers.

00:16.070 --> 00:22.190
As usual, before we consider how those helpers actually work, I'm going to use follow up in order

00:22.190 --> 00:27.290
to compare it to them and also demonstrate how convenient and faster approaches they are.

00:27.860 --> 00:36.410
For that, I'm going to create a right of persons in which I will insert some objects with properties,

00:36.410 --> 00:37.410
name and age.

00:38.330 --> 00:47.570
So right here, John and the age 28, let's duplicate it three times and also change the values, right,

00:48.020 --> 00:48.620
Mary?

00:50.400 --> 00:55.140
Than the age 20 then, right, Nick?

00:56.830 --> 00:57.850
And 15.

00:59.760 --> 01:04.290
And then finally, right, Jane, and the age 40.

01:06.270 --> 01:14.370
OK, so here we have persons with names and ages and we wonder if all of those persons or only some

01:14.370 --> 01:15.980
of them can vote in elections.

01:16.930 --> 01:22.360
And we're going to figure out in it according to their ages, for that, first of all, we will use

01:22.360 --> 01:25.960
follow up, but before that, I'm going to create variables.

01:26.900 --> 01:31.370
Let's call it every person can vote.

01:33.540 --> 01:37.290
As I default, let's assign to this variable through value.

01:38.750 --> 01:43.220
That creates another one and call it only some.

01:44.190 --> 01:45.960
Persons can vote.

01:47.780 --> 01:52.100
And in this case, as I default, let's assign to this variable false value.

01:53.380 --> 01:55.720
All right, now let's use follow up.

01:56.710 --> 01:58.660
Right, well, equal to zero.

01:59.920 --> 02:03.730
Then compare it to the length of a person's.

02:06.240 --> 02:07.560
And then I plus plus.

02:11.220 --> 02:18.540
So if one of those persons age is less than 18, then we want to change the value of every person can

02:18.540 --> 02:21.420
vote variable from true into force.

02:22.320 --> 02:27.810
And in the same way, we want to change the value of only some persons can vote variable from force

02:27.810 --> 02:28.500
into two.

02:29.520 --> 02:34.760
For that info, we need to create if statements which should have the following condition.

02:35.990 --> 02:39.260
If persons I dot h.

02:41.510 --> 02:42.800
Is less than 18.

02:44.450 --> 02:52.430
So if this condition is true, if one of the person's age is less than 18, then we need every person

02:52.880 --> 02:55.880
can vote equal to false.

02:57.590 --> 03:02.720
And at the same time, we need to assign to some persons can vote variable.

03:03.770 --> 03:04.760
Disvalue, true.

03:05.390 --> 03:07.850
All right, let's Log-in Council.

03:09.090 --> 03:13.260
Right here, every person can vote.

03:16.120 --> 03:17.290
Then the Desh.

03:18.230 --> 03:21.830
And place you every person can vote variable.

03:24.140 --> 03:26.360
And then again, running council.

03:28.570 --> 03:31.510
Only some persons can vote.

03:34.660 --> 03:35.200
Plus.

03:37.160 --> 03:39.230
The variable, only some persons can vote.

03:41.010 --> 03:48.150
All right, let's reload and see that every person can vote is false and only some persons can vote

03:48.150 --> 03:48.630
is true.

03:49.320 --> 03:56.730
Actually it's obvious because in person we have Nick, which is 15 years old, if we change it and writes,

03:56.730 --> 03:58.170
for example, 25.

03:59.780 --> 04:06.080
Then we will get the opposite values every person can vote is true, and only some persons can vote

04:06.080 --> 04:06.580
is false.

04:07.720 --> 04:14.710
OK, so using follow, we have figured out if every person from Iraq can vote or only some of them can

04:14.710 --> 04:16.870
vote, it's nice, it works.

04:16.870 --> 04:22.360
But as you notice for that, we have used a bit more logic and so much coding.

04:23.050 --> 04:27.800
Instead of this stuff, we are able to use every helper methods, every and some.

04:28.660 --> 04:35.110
At first, let's use every helper, which gives us boolean value true or false ID checks condition four

04:35.110 --> 04:36.040
of the items.

04:36.730 --> 04:40.290
And if the condition is true for all of them, it returns true.

04:40.810 --> 04:46.640
But if the condition is false, at least for one item, then it returns false.

04:47.530 --> 04:50.650
So create variable and call it every.

04:52.360 --> 04:58.600
Then assigned to it persons and yes, your every hour I help her with.

04:59.560 --> 05:05.390
And anonymous function and then insert here as a parameter person.

05:08.450 --> 05:12.200
Then, as you remember, we need written statements.

05:14.110 --> 05:19.930
Because of that, we wonder if a person can vote or not win at the following condition person, that

05:19.930 --> 05:24.430
age is greater than or equal to 18.

05:25.890 --> 05:28.080
Let's log in Castle to variable every.

05:31.610 --> 05:37.890
Then reload the page, and we have true because all persons ages are greater than 18.

05:38.570 --> 05:46.220
If we change one of them and writes, for example, for merry age 10, then we will get false.

05:47.480 --> 05:51.230
All right, let's consider in depth how every helper works.

05:51.800 --> 05:54.590
So here we have a person's array with objects.

05:55.550 --> 06:01.910
And also, every helper, whether it's a right to function on the first iteration function, is executed

06:01.910 --> 06:05.750
for first item John, his age is 28.

06:06.200 --> 06:10.940
Therefore, condition is true and every helper returns true value after first iteration.

06:11.970 --> 06:16.300
On the second iteration, function is executed for second object, Mary.

06:17.040 --> 06:19.980
The condition is still true because her age is 20.

06:20.580 --> 06:28.920
Every helper again returns true after that function is executed for third object, Nick, his age is

06:28.920 --> 06:29.430
15.

06:30.120 --> 06:32.730
So condition is false and we get false value.

06:33.960 --> 06:40.140
As soon as we get full value, every help or terminates situation and we get final value as false.

06:41.490 --> 06:48.060
You can imagine that every Harper uses logical and operator and you know that in case of an operator,

06:48.420 --> 06:51.770
if all conditions are true, we get through finally.

06:52.380 --> 06:57.080
But if at least one of the conditions is false, then we get false value.

06:57.900 --> 07:00.620
The exact same happens in case of every holper.

07:01.680 --> 07:08.250
Also, remember that as soon as every helper finds false value, it immediately terminates iteration

07:08.940 --> 07:12.260
because continuing execution of function won't make any sense.

07:13.220 --> 07:17.960
It's nice feature because it helps JavaScript engine to execute code faster.

07:18.890 --> 07:24.830
All right, let's go back to Brackett's and as usual, let's prove what I have just said for that.

07:24.830 --> 07:31.700
At first, I'm going to create variable X and assign to its value as zero.

07:33.150 --> 07:35.470
This variable actually will be counter.

07:35.880 --> 07:38.300
I want to show you what happens on each iteration.

07:39.000 --> 07:40.470
Let's run in council.

07:41.750 --> 07:50.390
Right here, plus plus X, I wrote plus plus increment operator before the variable X in order to get

07:50.390 --> 07:52.400
directly one instead of zero.

07:52.820 --> 07:57.110
If I wrote it after X, then we would get zero at first.

07:58.220 --> 08:06.070
Then, right, plus dash and then plus the condition person dot h.

08:07.670 --> 08:11.380
Is greater than or equal to 18.

08:13.030 --> 08:16.990
And also for Mary, changed back into 20.

08:18.990 --> 08:21.000
All right, reload the page.

08:22.020 --> 08:25.120
So you see, on every situation, condition is true.

08:25.590 --> 08:32.310
That's why we have four iterations and also that's the reason why we have true value as a result if

08:32.310 --> 08:34.800
we change the age of marriage into 10.

08:37.170 --> 08:44.370
Then we will get on first iteration through value, add on second one, false also you see that third

08:44.370 --> 08:46.890
and fourth iterations are not executed anymore.

08:47.220 --> 08:51.570
And because of the false value on second iteration, finally we have false.

08:53.320 --> 08:57.530
All right, I think that regarding every Harpur, everything is clear for you.

08:58.300 --> 09:00.600
Let's go ahead and talk about some helper.

09:01.420 --> 09:09.400
So here we have a right of persons, but in this case with different ages, also here we have some helper.

09:10.560 --> 09:17.880
On the first iteration, function is executed for first object, John, his age is 10, therefore condition

09:17.880 --> 09:20.400
is false and somehow operates on false value.

09:21.330 --> 09:24.010
Unlike every helper, it continues and moves on.

09:24.040 --> 09:27.930
Second, iteration function is executed for second object.

09:27.930 --> 09:33.120
Memory condition is still false and again, somehow Puritan's false value.

09:34.130 --> 09:40.580
Then function is executed for third object, Nick, in this case, condition is true because his age

09:40.580 --> 09:44.840
is greater than 18 and somehow Puritan's true value.

09:45.890 --> 09:52.500
As soon as it finds true value, it terminates iterations and gives us, as a result, value.

09:52.560 --> 09:58.730
True, I like every helper some uses logical or operator.

09:59.760 --> 10:02.590
So, again, somehow project's condition.

10:03.030 --> 10:10.920
And as soon as it finds true value stops iterations, but if condition is false for all items, then

10:10.920 --> 10:14.130
this is the only case when somehow Harpur returns false value.

10:15.450 --> 10:16.110
Makes sense.

10:17.010 --> 10:22.830
All right, let's go back to Brackett's and change everything into some.

10:24.140 --> 10:26.510
Also, change the condition and write.

10:29.580 --> 10:32.640
Person, that age is less than or equal to 18.

10:34.360 --> 10:42.220
Revote and we got on first iteration false value add on second iteration, we have to do so on second

10:42.220 --> 10:47.530
iteration somehow found true value and as a result it returns true.

10:48.340 --> 10:55.990
If we change the age of marry and write 20, then we will get false in all four iterations and for a

10:55.990 --> 10:57.150
final result as well.

10:58.980 --> 10:59.460
All right.

11:00.570 --> 11:04.500
It was all about average and some helpers see you in the next video.
