Let's move on to the next hour, helpers, in this lecture.
I want to introduce you every and some helpers.
I decided to demonstrate those helpers in one video because they work in a similar way as it was in
case of filter and find helpers.
As usual, before we consider how those helpers actually work, I'm going to use follow up in order
to compare it to them and also demonstrate how convenient and faster approaches they are.
For that, I'm going to create a right of persons in which I will insert some objects with properties,
name and age.
So right here, John and the age 28, let's duplicate it three times and also change the values, right,
Mary?
Than the age 20 then, right, Nick?
And 15.
And then finally, right, Jane, and the age 40.
OK, so here we have persons with names and ages and we wonder if all of those persons or only some
of them can vote in elections.
And we're going to figure out in it according to their ages, for that, first of all, we will use
follow up, but before that, I'm going to create variables.
Let's call it every person can vote.
As I default, let's assign to this variable through value.
That creates another one and call it only some.
Persons can vote.
And in this case, as I default, let's assign to this variable false value.
All right, now let's use follow up.
Right, well, equal to zero.
Then compare it to the length of a person's.
And then I plus plus.
So if one of those persons age is less than 18, then we want to change the value of every person can
vote variable from true into force.
And in the same way, we want to change the value of only some persons can vote variable from force
into two.
For that info, we need to create if statements which should have the following condition.
If persons I dot h.
Is less than 18.
So if this condition is true, if one of the person's age is less than 18, then we need every person
can vote equal to false.
And at the same time, we need to assign to some persons can vote variable.
Disvalue, true.
All right, let's Log-in Council.
Right here, every person can vote.
Then the Desh.
And place you every person can vote variable.
And then again, running council.
Only some persons can vote.
Plus.
The variable, only some persons can vote.
All right, let's reload and see that every person can vote is false and only some persons can vote
is true.
Actually it's obvious because in person we have Nick, which is 15 years old, if we change it and writes,
for example, 25.
Then we will get the opposite values every person can vote is true, and only some persons can vote
is false.
OK, so using follow, we have figured out if every person from Iraq can vote or only some of them can
vote, it's nice, it works.
But as you notice for that, we have used a bit more logic and so much coding.
Instead of this stuff, we are able to use every helper methods, every and some.
At first, let's use every helper, which gives us boolean value true or false ID checks condition four
of the items.
And if the condition is true for all of them, it returns true.
But if the condition is false, at least for one item, then it returns false.
So create variable and call it every.
Then assigned to it persons and yes, your every hour I help her with.
And anonymous function and then insert here as a parameter person.
Then, as you remember, we need written statements.
Because of that, we wonder if a person can vote or not win at the following condition person, that
age is greater than or equal to 18.
Let's log in Castle to variable every.
Then reload the page, and we have true because all persons ages are greater than 18.
If we change one of them and writes, for example, for merry age 10, then we will get false.
All right, let's consider in depth how every helper works.
So here we have a person's array with objects.
And also, every helper, whether it's a right to function on the first iteration function, is executed
for first item John, his age is 28.
Therefore, condition is true and every helper returns true value after first iteration.
On the second iteration, function is executed for second object, Mary.
The condition is still true because her age is 20.
Every helper again returns true after that function is executed for third object, Nick, his age is
15.
So condition is false and we get false value.
As soon as we get full value, every help or terminates situation and we get final value as false.
You can imagine that every Harper uses logical and operator and you know that in case of an operator,
if all conditions are true, we get through finally.
But if at least one of the conditions is false, then we get false value.
The exact same happens in case of every holper.
Also, remember that as soon as every helper finds false value, it immediately terminates iteration
because continuing execution of function won't make any sense.
It's nice feature because it helps JavaScript engine to execute code faster.
All right, let's go back to Brackett's and as usual, let's prove what I have just said for that.
At first, I'm going to create variable X and assign to its value as zero.
This variable actually will be counter.
I want to show you what happens on each iteration.
Let's run in council.
Right here, plus plus X, I wrote plus plus increment operator before the variable X in order to get
directly one instead of zero.
If I wrote it after X, then we would get zero at first.
Then, right, plus dash and then plus the condition person dot h.
Is greater than or equal to 18.
And also for Mary, changed back into 20.
All right, reload the page.
So you see, on every situation, condition is true.
That's why we have four iterations and also that's the reason why we have true value as a result if
we change the age of marriage into 10.
Then we will get on first iteration through value, add on second one, false also you see that third
and fourth iterations are not executed anymore.
And because of the false value on second iteration, finally we have false.
All right, I think that regarding every Harpur, everything is clear for you.
Let's go ahead and talk about some helper.
So here we have a right of persons, but in this case with different ages, also here we have some helper.
On the first iteration, function is executed for first object, John, his age is 10, therefore condition
is false and somehow operates on false value.
Unlike every helper, it continues and moves on.
Second, iteration function is executed for second object.
Memory condition is still false and again, somehow Puritan's false value.
Then function is executed for third object, Nick, in this case, condition is true because his age
is greater than 18 and somehow Puritan's true value.
As soon as it finds true value, it terminates iterations and gives us, as a result, value.
True, I like every helper some uses logical or operator.
So, again, somehow project's condition.
And as soon as it finds true value stops iterations, but if condition is false for all items, then
this is the only case when somehow Harpur returns false value.
Makes sense.
All right, let's go back to Brackett's and change everything into some.
Also, change the condition and write.
Person, that age is less than or equal to 18.
Revote and we got on first iteration false value add on second iteration, we have to do so on second
iteration somehow found true value and as a result it returns true.
If we change the age of marry and write 20, then we will get false in all four iterations and for a
final result as well.
All right.
It was all about average and some helpers see you in the next video.