In the last section, we started introducing the every and some helpers, and we did that by creating
this array of computers, each computer had a certain amount of ram and then we said that we have a
program that requires at least 16 gigabytes of RAM.
So we wanted to figure out, rather than finding just a list of the computers that could and could not
run the program.
I wanted to get just a boolean value and say, can all of them are on the program or at least can some
of them run the program?
And so we first did that with the for loop and there was a decent amount of logic in there and we had
to fiddle around with the defaults of these billions trying to figure out what's going on the very end.
We eventually ended up with a refactored snippet of code down here where we use the every helper.
So with every helper, we have an iterator function.
And inside the iterator, we have to return a boolean value if every single element returns.
True.
Out of this expression right here, the every helpful helper will return.
True, but if any element returns false, then the whole expression will return false as well.
And so we saw that with the diagram over here of every so we had twenty four in return.
True 30 to return.
True for return.
False however.
And then at the very end of the day, out of every element we could kind of imagine that internally
it's doing an and and and and looking at all these different internal values to eventually produce a
single result of false in this case.
So we could have easily made this thing pass by saying, does every computer have at least greater than
two gigabytes of RAM?
And now you can see, OK, sure enough, yes, every computer has at least greater than two gigabytes
of RAM, but we really want to deal with 16 here.
So again, I know I'm really harping on this.
I know you're probably getting tired of me telling it or saying it.
But up here, kind of unclear logic.
We've got a hefty number of lines, looks about 11 lines of code or so.
Whereas down here with the every helper, this is three lines of code.
And the benefit to this is engineer any engineer who looks at this in the future.
They're going to see that keyword right there.
They can see the word every and they're going to say, OK, every I've got a reasonable idea of what
the every helper does.
And so I've got a reasonable idea of what this function or what this expression right here returns is
probably in return a true boolean value.
So, again, I'm just a huge proponent of using these array helpers over using for loops or anything
like that.
So let's continue on now and wrap up this example by implementing our some helper as well.
So the some helper, let's check out a diagram, works very much like that, every helper.
And again, that's why we're covering both these at the same time.
So with every computer we pass in the computer into an iterator function and it will return a boolean
value.
So up here in the 16 case, we return, true four is less than excuse me, 16.
I'm at say twenty four.
Twenty four gigabytes of RAM.
Just be really clear when we have four gigabytes of Ram that's going to turn false and then thirty two
is going to return.
True because it's greater than the sixteen gigabytes of RAM we need.
Now the difference between every and some with every we look at all the values that get returned and
we kind of imagine that there is an an operator between each one, but with some which we're looking
at right now, there is an or operator between any value.
So with some we're saying, do any records in this array satisfy this criteria?
Do any records have greater than sixteen gigabytes of RAM?
So yep, at least one does over here and we've got the end operator is eventually going to return.
True.
So everything else can return false but at least one record return.
True.
So with some help we would expect to get true out of it.
So let's put this example together.
In practice, the code is going to look nearly identical to every helper.
So we'll put in our iterator function and will return computer program greater than sixteen.
And in this case, again, at least one computer does have greater than sixteen gigabytes of RAM.
So we get a result of true from this.
Again, do not forget to put this return.
Key word in here.
Just the most common problem.
I see it all the time.
I didn't make that mistake myself.
If you return, if you forget the return keyword, the function is going to return undefined and your
every person, your some helper is going to think, oh, OK, well, is returning undefined from every
iteration through the array.
So I guess no computers match the criteria is looking for.
So let's not forget that return keyword in there.
OK, so I know that between this video and the last one then a little bit long.
Let's continue in the next section with one more example of every and some and then we'll work into
some exercises after that.
So I'll catch you in the next section.