So we're getting closer
to the end of this section
but there are still a couple of array methods left to learn.
And so in this lecture,
we're gonna look at the some and the every methods.
And to start learning about the some method,
let's look back at the includes method
that we studied earlier.
So let's log to the console here movements.includes -130.
And let's also log our movements array so we can see it
and so indeed, the value of -140 that we're testing here
is in the array
and that's why we get true here.
And so we can use the includes method
to test if an array includes a certain value.
Now, however, we can only really test for equality, right?
So basically includes here returns true
if any value in the array is exactly equal to -130, right?
And so again, this is essentially testing for equality
but what if we wanted to test for a condition instead?
And so that's where this some method comes into play.
So let's say that we would like to know
if there has been any deposits on this account.
So in other words, we want to know
if there is any positive movement in this array.
So any number above zero.
So how would we do that?
Well, we can say movements.some
and now we can specify our condition here as always.
So the same callback function,
which has to return either true or false.
And so a deposit is a movement greater than zero.
So let's call this here anyDeposits
and log it to a console.
And so indeed here we get true.
So we have, of course, more than one deposits here.
But let's say we wanted to test
if there is any deposit above 5,000.
So easy enough, we can check if there is something
above this value and then it is false.
Or we can check if these is something above 1,500.
And so now it's true because there is one movement here
in that condition.
And so you can see that these two methods here
are kind of similar.
The difference is that here it checks only for equality.
So equality but here, we can specify a condition now, right?
So basically, this one here,
we could rewrite it like this.
So let's log it to the console here.
So movements.some,
minus 130.
So this would be exactly the same.
And so that's why we also get true in this one.
Now, for equality, this doesn't make much sense.
We could then simply use the includes method
but for the case we need a condition like this one here,
or really this one,
then the some method is perfect for that, okay?
And probably if I would have named this method,
I would have called it like any.
But of course, that's not the real name
but that's really what it does.
And so if there is any value
for which this condition is true,
then the some method will return true.
Now, right and now let's actually use this some method
to implement our missing functionality,
which is to request a loan to the bank.
So we will do that right here
between the transfer button and the close button.
And the some method will become helpful
for this loan feature
because our bank has a rule,
which says that it only grants a loan
if there at least one deposit
with at least 10% of the requested loan amount.
So that sounds complicated
but we will implement this here in a second.
So btnLoan.addEventListener click
and this is now always the same as you can see.
So starting by preventing the default action,
so that's always gonna be the first thing
and then let's have a look here at the user interface
because right now it is hidden.
So you see that here we only have this one field.
And this field is called the inputLoanAmount, okay?
So inputLoanAmount, so that's the element
with this long class name.
So let's copy that actually.
And so let's start by actually getting that amount here.
So we already know we will have
to convert this input .value, okay?
And now we need to create our condition here.
So first of all,
that amount should be greater than zero, okay?
And now let's take a look here at our flow chart again.
So this is what I was saying earlier.
The loan is only granted if there is any deposit
that's greater or equal 10% of the requested amount of loan.
Now, right?
So here we need now our second condition,
which translates that.
And so whenever we see or hear the word any,
we can already know that it's probably a good use case
for the some method.
So let me show it to you.
So as always, we get the movements
from the currentAccount object .movements
and now on there, we call the some method.
So we already know by now
how this callback function feature here works.
So we get the current movement
in the current iteration
and now here the condition is that movement needs
to be greater or equal than 10% of the amount.
So that's the amount divided by 10.
Okay?
Or we could also do times 0.1
and so that's exactly the same thing.
So 0.1 here stands for 10%
and so let's keep it like this.
Give it a save so that it looks nicer
and so yeah, if at least one of the elements
in the movements array has this condition,
so it's true, so basically it's greater than 10%
of the requested amount,
then all of this here will become true.
And so the some method is perfect
to be used in a condition like this.
So when we need to test for something, okay?
So in case this is true.
Oh and I closed our flow chart again.
So in case this is true,
then add a positive movement to the current data
and update the UI.
So that's easy enough.
So add the movement here.
So currentAccount.movements.push
and then of course, the amount that was requested.
And then update the UI
and for that, we already created our nice function,
which does all of the functionalities in one go.
And so that's updateUI
and we simply pass the currentAccount into it.
Okay.
And that's it.
Finally, let's also then clear the input field.
So that value equals empty value.
So we did that here and here and also up here.
Okay.
Let's log in.
And so you see, my deposits, my largest deposit
is 3,000 and so the loan that I can request
can be maximum 30,000, okay?
Because 3,000 here is 10% of that.
So let me try something bigger.
So something really huge.
And so now nothing happened.
And again in the real world,
we would then probably get some error message
somewhere here at the top or so
but in this case, we're not gonna do that.
Let's just try 5,000 now.
And so now indeed it appeared here in our movements
and also as you see, our balance was updated
and also here our total income.
Okay?
Now let's request 50,000
and so with this, you can now actually cheat.
You can always create a little bit of a larger number.
So now I can even request half a million
and so that's cheating our algorithm,
which is way too simple.
But of course, this is just for testing purposes.
All right.
So that's how the some method works.
I hope that makes sense.
And so let's now actually go back here
and talk about the close cousin of the some method,
which is the every method.
So let's write here some
and every.
So again, the every method is pretty similar
to the some method
but as you might guess, the difference between them
is that every only returns true
if all of the elements in the array satisfy the condition
that we pass in.
So in other words, if every element passes the test
in our callback function,
only then the every method returns true
and that's why the method is called every
in the first place.
So let's test it out here now.
So let's log to the console movements.every
and then with the same condition.
So let's essentially check
if all of our movements here are deposits
and well, indeed, they are not
and that's why we get false.
However, we do have one account,
which only has positive moments.
So let me show that to you.
That's account4.
So account4, all the movements are positive.
So let's check it out now.
So account4 and then the same condition
and every is not a function
and that's, of course,
because we are still missing the movements array.
And now it is true, all right?
And so that indeed proves that the every returns true
if all the elements in the array satisfy this condition
because in this movements array,
all of the values are in fact above zero, okay?
So that's how the every method works
and I think it's quite straightforward, isn't it?
And now to finish, there is now more cool thing
that I want to show you.
So up until this point,
we have always written the callback function directly
as an argument into our array methods, right?
However, we could also write this function separately
and then pass the function as a callback.
So let's say
separate callback
and so we could do this.
So let's call deposit to this function.
All right?
And so this function here is exactly the same as these ones
but as I just said,
there is no reason for them to being directly written here
in all of these array methods.
We could simply write them like this
and then all we would have to do
is to call movements.some, for example,
and then deposits or deposit.
And now we could reuse the same function
for all kinds of different methods
that require callbacks with a true/false condition.
So that could be every or filter as well.
Okay?
And so here is the result of these three operations
and so here we get the expected results.
And all by reusing the same function.
Then if we wanted to change the function,
all we would have to do is to change it here in one place
and then all the results
would become different according to that.
So in practice, that's something that we do sometimes
because this is, of course, better for the DRY principle.
So don't repeat yourself.
That's always important and it is important here as well.
But anyway, with this small detour that we did here,
let's now move on to learning about two more array methods.