Alright guys, I know we haven't actually done anything useful in the webpage for
a little bit, and it's going to be a little bit longer because we need to talk about
a few more JavaScript things first. So think of JavaScript like a programming
language. There are objects, functions, variables, and a lot of other built-ins
that we're going to discuss as well. What we're going to in this video, we're going
to create a simple function that's going to get a missing number from an
array. So, project time. If you guys want to pause this video and attempt that
right now, that's probably not going to work, actually. I don't think we've talked
about sorting arrays, so that's something we're going to do right now. So, first we
need to create an array. So, variable numbers is going to be a list, and
we're going to put them in a jumbled up order. So 43, 56, 601, 100, 45, 10, 58 and 34.
So, this is a jumbled up list, or array, and we can use the sort method. I'm going
to save this and refresh the page, and we're going to do this in an interactive way.
We have numbers here. Wou can go numbers.sort, and it's a method so you write
it like that. Now, there is a problem with this and this is that it's going to sort
as a string. So let me create words, and we're going to say apple, pear, coconut, I
probably spelled that wrong, pizza...I guess it's not just going to be fruit,
it's going to be foods, hamburger, and banana. Now, this is going to sort it
correctly because it's going to do an alphabetical sort.
Now if you know how to alphabetize stuff, and I'm going to assume that you do, it
first looks at all the first letters, and it arranges them. And when it gets here it's
going to see there's two items with the beginning letter P so
it's going to move on to the next one, and it's going to say okay, P comes
before I, so pair is going to go before pizza, and it continues on. Let's say the
first five letters of a word are the same, it's going to move on to the sixth
to determine the direction that it should be sorted. So, that's not
just on, that doesn't only relate to JavaScript, that relates to alphabetizing
in general. I'm going to refresh this and we have words here. So if I type words.sort...
and the sort method is available on all arrays, so this is an
array method. It's built-in and it is automatically is available. So when I do
this it sorts it correctly because it's doing an alphabetized sort. So, when we're
using numbers it's trying to do the same thing. So if we see here a hundred is put
before thirty-four because one is smaller than three, and so on. So, that as
you can see this is doing it wrong so we need to supply a parameter to the method
in order to properly sort numbers, and it's a function parameter. So I'm going
to go ahead and write numbers.sort and then open up the parentheses. Now in
here we're going to write a simple function that takes A and B and we
return A minus B, or B minus A...okay, let's just go do it.
So, pass in function, this function takes a and b, and then we open up curly braces
and we say return a-b; I'm going to... I don't need to save that. I forgot I'm
working in in the interactive console here. So when I hit enter it's going to
properly sort the numbers. So, that's how you numerically sort an array. It's just
a simple line of code here. So, what we're going to do is pause the video now. I want
you guys to create a list with a range of numbers, let's say 1 to you know 20,
just for simplicity. I don't want to have you guys create like this full-on array
of numbers that's like you know a hundred, one hundred indexes or so. So just a list
from from 1 to 20, and then make one number that should be in that list not
in that list. So let's say the missing number would be 6. So you guys are going
to, in your code editor, you're going to create an array with a missing number,
and you're going to write a function that's going to determine what the
missing number is. Now, the the array should have sequential numbers, so your
list including the missing number should look something like this: 1, 2, 3, 4,
7, 8, 9, but I want you to put it in a jumbled up order. So, it would look
something like 9, 7, 2, 3, 5, 6, etc. and then you would just make sure that one number
was not in there, alright? So, I'm actually going to write down a list that I'm
going to be using so that you guys can write the same thing just in case I
didn't explain that well enough. Let's remove the words, and let's start writing
the list. So...
do we have 5 in there, nope. Do we have 2, no. Alright, put a 6 in there, so 16, 18, this
is going to be a little more confusing than I thought.
Alright, where's that comma? Alright, so, we've got 1, 2, 3, 5, 6, 7, 8, 9, 11, 10, 12, 13, 14,
15, 16, 17, 18, 19, and 20, alright. So, these are all our numbers.
Now I'm going to select one at random here, and just to make sure these are
correct and that they're sequential numbers, I'm
actually going to refresh the page here and I'm going to write numbers.sort,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14...18, 19, and 20.
Alright, so, we're missing the number 4, and we have 18 in there twice. I'm going
to remove an 18 there and our missing number is 4. Pause the video, write this list out
like this it's going to be a lot easier, and then write that function that
determines the missing number. And then when you're done
proceed on to the next video.