We saw in the last lessen some of the really neat things that you can do with the race, so there are
even more things that you can do, such as sorting them.
You can reverse sort them, you can concatenate them together as well as find objects in the array.
And we also looked at index of we're going to look at that in a little bit more detail.
So, again, going back to what is available at the Mozilla Developer Network.
So if you go into the page, you can see on the left hand side, there's all of these different methods
that are available with arrays.
You can join them, you can reverse them.
So we are going to be looking at some of these in more detail in this lesson.
So here's a code snippet and we've got a couple of different arrays.
So what we're doing is first we're sorting them, then reverse sorting them.
So that will reorganize and reorder these arrays.
And the values are contained within the arrays.
Also looking at to check to see if there's an index value.
So this is where we're returning back a value of negative one.
So that means it's not found in the array joining to arrays together.
So we've got my array concat, rounded brackets, my array two.
So that's joining those two arrays together to form a new array or outputting that as well as checking
to see if an element is available within the array.
So we're finding it and returning that element.
And we can also add in and have a condition there to check to see if the element is greater than 10.
Then we return that value and we can also output that found value as well into the console.
So find is also really powerful because it gives us the ability to find items that are contained within
the array and return them back.
So this is going to return back the first item within find.
There's more information, of course, Mozilla Developer Network.
So within their example, they have Horeb, all different numbers in there.
And we're looking to see to find if the value of the element.
So we notice that we've got a function in there.
So we're got an argument that's being passed into the function.
So all of the different values are contained within the array.
It will return back the first one that it finds that's greater than 10.
So in this case, it found 12 and that's where we get an output of 12 is the challenge of the lesson
is to transform the array.
So try out the different methods, sort in reverse.
Also combine two different arrays together with the CONCATENATE method and then search the array for
a number and return a value that matches that number that is greater than the one that we're trying
to find.
And you can also console log out the elements as you're iterating through and you can see what gets
output into the console.
So go ahead and try this out.
So let's start with the default where we've got to arrays, then console log out the my array, then
taking my array.
We're going to sort it.
And then also we're going to output the array after the sort.
So see what the differences.
And as you can see, when it gets sorted, it gets sorted with numbers first.
So the numbers starting at the lowest to the highest and then it starts alphabetically sorting the array
items from a all the way to W.
So all of those gets sorted alphabetically as well.
So that's what sort does you can also do our reverse sort.
So that's another option where you can reverse the order of the items in the array.
And what this will do is this will do actually the opposite of sort of last one.
So starting with alphabetically w all the way down and then starting with numbers, we're going all
the way down with the numbers.
Let's check to see if it contains a value.
And this is where we can use the index of again where we've got the my array and checking to see index
of the value that we're going to look for, for the index.
So how about we look for hello?
And if the index value is negative one because it's an index of negative one.
So if we say that it's not equal to negative one, we see that we've got item found.
Update this to the world.
How about we call it World two and refresh?
We get not found.
Let's create a new array joining to a race together.
So we've got a variable that we're going to declare a new array.
So taking the mihiri and then using concat concat will allow us to join the two arrays together and
if we can't so log out the new array value, you're going to see that those are both arrays that are
joined together.
So it's a really long array.
Got 15 items in length there for this array.
So do let found and then the array that we want it to search so we can use that my array and looking
at find that we've got the function and we've got an element as the value that we're passing in.
So we can see that and then we'll also return if L is greater than 10.
And so this isn't the numeric race.
So maybe first we'll try the numerical and then we'll try the other one afterwards.
So see, what happens here is that as we loop through the array values, so once it hits a value that's
over 10, then it stops.
So if this value let's add in a few other ones now we can see it goes through all of the different values.
So it starts at five.
It looks at it's got an undefined because I've got a blank space there.
So essentially what it's doing is it's looping through until it finds the one that matches the criteria
that we're searching for.
And that's how you can use find in order to return back values.
So if we try the find on my array, you can also see that it goes through all those values.
So it is looking at all those values until it finds one that has a value of ninety nine.
And when it does, then that's the one that returns back.
So first one to meet the criteria then that's the one that gets returned back.
If we don't meet the criteria for looking for over one hundred, you're going to see that it's going
to logout that undefined.
So it loops through all of them and it returns back undefined.