Jonas: Okay, so now we know about find and findindex,
but there are actually two additional methods
that were added in ES 2023,
and those are called findlast and findlastindex.
So these are basically counterparts to find and findindex
because as their name says,
they do the exact same thing as these two,
but they start searching from the last to the first element.
Now, let's just, as always,
start by logging our movements here,
so that later we actually see if our results are correct.
And so now, using the findlast method,
I want to determine the last withdrawal,
so lastwithdrawal.
So it's a bit hard to type, but we can do it.
So again, now on movement,
we're going to not call the find method, but findlast.
And so, as we already know,
a withdrawal happens whenever the value of the movement
is less than zero.
So this kind of condition that we're writing here now,
we have written it many times before, so nothing new here.
So less than zero,
and then let's log it to the console as well,
so lastwithdrawal.
And so, as expected, we get -130,
because now the method is starting to look here
from the end of the array.
So it's basically finding the last value
that is less than zero, which is this one right here.
If we were, just like before, using the find method,
then we would get this one right here.
So the first starting to look from the beginning, so -400.
So, that's actually it about this method.
As defined, lastindex is also pretty simple,
so it's basically just like findindex.
So let's do a nice small challenge here,
and actually, if you want,
you can really pause the video and do this as a challenge,
but we can also just do it together here if you like.
So what I want to do is to use this method
to print a string like this.
So your latest large movement was,
and then a number here, X movements ago.
And notice the part of ago here,
which implies, again, that we're counting from the end.
And so basically, these two methods,
so findlast and findlastindex are very, very useful
when we're dealing with sorted data
and are interested in getting the most recent occurrence
of something in this array, or basically in other words,
the latest addition to the array.
And so here, we're also basically assuming
that these movements here happen in order,
so first this one, then this one, this one,
and so on and so forth.
That's why here we said that
this one here was the last withdrawal,
so in this ordered array of movements,
this one here is indeed the last.
And so again, for finding the last occurrence
or the most recent occurrence,
it is useful to start looking from the end,
and so that's when findlast and findlastindex become useful.
And so here in this case,
we're basically gonna look for how many movements ago
the last large movement happened,
and with large, we mean greater than 2000 euros
or dollars or whatever it is here.
All right, so again, if you want,
you can do this as a challenge,
or we can just do it together here.
So let's do that now.
So let's find the index
of the latest large movement here first.
So, latestlargemovementindex,
and again, I like to use these pretty long variable names
just because they're so descriptive.
If someone else reads this code in the future,
they will know exactly what kind of value
is stored in that variable.
So, findlastindex.
And then here again, we get all our movements,
and now here, it doesn't really matter
whether this movement was a withdrawal or a deposit,
it's just a movement.
So we're not interested here in the sign of the number,
so whether it is positive or negative,
so we just take the absolute value of it.
So this we already did before,
so just the value of the movement
needs to be larger than 2000 here.
So let's check if we got this correct.
And so, the result is three, so zero, one, two, three.
Now in this case,
actually it would've been the same result
using findlastindex or findindex
because there is only one value
that satisfies this condition.
So let's actually use greater than 1000
because in this case,
this 1300 here is also a large movement.
And so, then the last index should be this one,
and indeed, now it is number seven.
So zero, one, two, three, four, five, six, seven.
Great, and now let's use this value
to lock this string here to the console.
So let's grab this from here,
and let's leave that there, why not?
Console.log like this,
and let's make a template literal here.
But here, we cannot just plug in this value like this.
So latest.
Well actually, this is not the correct name here,
so latest, it was a typo there.
Right, because the latest large movement
was not zero movements ago,
but really just this one movement ago,
so basically, the last movement here.
And so, we can just subtract this value here
from the total length of the array,
so to get the value that we want here.
So again, this now has nothing to do
with this method here anymore.
This is just us trying to get the value that we want
to solve this challenge that we set for ourself.
So, movements.length
and then minus this,
which gives us was one movement ago.
But let's assume that this here is actually zero movements,
so that if you want to say that it happened just now.
So since the index is zero based
but the length is really eight,
so the length of the array is eight,
but this last element is number seven.
We just need to subtract always just one more here.
And so, it was zero movements ago.
If you were to go back
to the initial thing that we had here,
then it would be four movements ago,
so zero, one, two, three, four.
And maybe it would make a bit more sense,
if this was actually five movements ago,
and this one here was one, as it was initially.
So let's just get rid of this part again,
and this is a bit more natural.
So now, this was five movements ago,
and if it was like this, if this was the large movement,
then it would be one movement ago.
This one right there.
Now, all right, and this is all for this lecture,
and so let's move on to some more array methods.
We're still not done here.
There's a lot to come.