Alright guys, good stuff. Hopefully you guys manage to do that. If not, it's okay. If
you did, good job! It probably took you multiple times unless you come from a
programming background and have a history of writing algorithms. This is a
very simple one, and so if you want to see my take on it, keep watching. If you
did it successfully, then you know you don't have to watch this, but if you do
want to then go for it. I'm going to create a function first
obviously that's going to compute this. So, function...actually, target my editor.
Function and I'm going to call it missing number, and we're going to pass
in numbers. So, what we're going to do is I'm going to set a missing number
variable, so var missing right now it's going to be set to minus 1. Then what we
need to do is, we need to get the last index of the array, and then the first
one, and we loop through until we find the missing number. So, we're going to use
a for loop, we're going to set i equal to numbers[0], it's going to be the first...
actually we're just going to set it 0.
This is going to exist until i is less than, or as long as i is less than or equal to
numbers linked, and then at the end of each loop we're going to say i++.
So this is our loop, it's going to run 19 times, and it's going to find the missing
number. So, what we need to do is just check if this i is in the numbers array
then you know pass. Otherwise, if it's not, return that it's not. So we're going to
be using index of. So, if index...sorry, numbers.indexOf
of i, it's going to be equal to -1 which means it is not found in numbers, we set
missing equal to i, and then down here we're just going to return that variable.
It's literally that simple. Let's save it and we need to now call it down here. So,
missing no and we're going to pass in this numbers. Save it,
refresh, I need to call that, I need to log that, actually. Console.log and remember
that our missing number was 4, and it found it correctly. So, I'm going to walk you guys
through this a little bit more, this actually isn't created so let's get that
looking a little better. Alright, so, we have a list from 0 to 20 and it's
missing the number 4, so the length of this is 19, but if all numbers were there
the length should be 21, or sorry the length is 20 but the length should be 21
because we've got 0 in there as well. We don't need to use the sorting that I
explained in the last video, but that's another way to do it. It's more complex...well, it's
simple, but it's a lot more code. So if you guys did it that way then good stuff.
What I've done here is we pass this jumbled up array that's missing one
number into this function, and we set missing to minus 1 and this is just
basically for it to have a default state, and minus 1 is nowhere in here so it's
kind of irrelevant. If this will return, if this were to return as minus 1 we
would know that no number is missing. So, for instance, if I put 4 in there and
save it, and refresh, it's going to return us 21 because...alright. Where we've
got zero in there, I need to do this. This is
going to make it run 20 times. So I'm going to save it and refresh, and we get minus
1. So, that's because it didn't find the number and that's why, that's
why I put that there. Let's take out the number 15, run it once more,
save it and refresh, and we get 15 there. So what happens is we create our loop, we
use the lowest number in the array, and if we didn't know how many items
were in the array and where to start, you know what the first number was let's say
it started at 3, we should be doing it this way. So, we are going to sort it for
this case, var sorted equals numbers.sort, and then in order to sort
numerically we have to write that little a, b code here. We return a minus b. So,
what we're going to do is we're going to set the default i value as sorted[0].
So let's remove the numbers 0, 1, and 2, so now the array starts at 3.
Save it and refresh, and it still returns 15, and that's because this starts at 3
as well. So it goes from 3 until the item is you know the last item in the
array, so anywhere between that range we can do this. And then what we do is we
check if the array here has this sequential number in it, or rather
doesn't have it, then we set missing to that because we know that i here is
counting sequentially. We know that's always going to be true. So, this is going
to have every number between 3 and 20. That's just the case, that's just the way
it is. So then what we do is we just set missing equal to that number, and then
outside of that loop we return the missing number. So, good job guys! This
is a little bit more complicated, and this is the kind of problems we're
going to get into solving more. If this seemed hard to you don't worry about it.
I'm going to make it so that stuff like this is not difficult, and you're going
to be able to figure this stuff out pretty quickly, and you're going to know
how to, actually. Figuring this out, figuring out what you need to do, is a
very small part of this. Knowing what kind of loops and what methods to run in
JavaScript to solve this problem, that's what I'm
going to you know make sure that you guys are comfortable with doing. So in
the next video we're going to get more into objects, and the different ways we
can create those and work with those.