WEBVTT
00:00:01.238 --> 00:00:04.395
You already learned about using variables
to store numbers or strings.
00:00:04.395 --> 00:00:06.245
Now we're going to learn
about something called arrays,
00:00:06.245 --> 00:00:09.675
which let us store multiple items
in just one variable.
00:00:09.675 --> 00:00:12.980
As you'll see, arrays let us do
all kinds of useful things.
00:00:12.980 --> 00:00:14.622
Okay, let's review variables. All right?
00:00:14.622 --> 00:00:18.531
So we have, var myFriend = "Sophia".
00:00:19.761 --> 00:00:22.640
So, a variable is just a way
of storing a value, like Sofia,
00:00:22.640 --> 00:00:26.600
and giving it a label so our program
has an easy way to refer to it later.
00:00:26.600 --> 00:00:28.109
We can think of it like a drawer,
00:00:28.109 --> 00:00:32.389
with a myFriend label on the outside
and Sofia on the inside.
00:00:32.389 --> 00:00:35.118
So whenever we look inside it,
we find Sofia.
00:00:36.198 --> 00:00:39.946
Now, sometimes we want to hold
multiple values in a single variable
00:00:39.946 --> 00:00:44.191
and we can't just do it like this
because we'll get a syntax error
00:00:44.191 --> 00:00:46.271
and our program will freak out,
and all that stuff.
00:00:46.271 --> 00:00:48.582
So we have a specific way
that we do that,
00:00:48.582 --> 00:00:50.572
and that's using arrays.
00:00:50.572 --> 00:00:53.189
So we could say, myFriends = ,
00:00:53.189 --> 00:00:55.729
and then we have to do
a square bracket,
00:00:55.729 --> 00:00:58.599
and then inside,
that's where we can put all the values.
00:00:58.599 --> 00:01:03.716
So we say, Sofia,
and we have John, and we have Leif.
00:01:03.716 --> 00:01:06.338
All right, so those are my three friends.
00:01:06.338 --> 00:01:09.384
Maybe in order of how much I like them,
but don't tell them that.
00:01:09.384 --> 00:01:12.984
Okay, so now our variable
holds three values, not just one.
00:01:12.984 --> 00:01:15.191
And we can imagine
it's like a chest of drawers,
00:01:15.191 --> 00:01:18.191
and we put this label, myFriends,
on the whole chest.
00:01:18.191 --> 00:01:20.369
And we can just open the right drawer
00:01:20.369 --> 00:01:22.669
to find the value
we're looking for, right?
00:01:23.689 --> 00:01:24.875
And you imagine, with a chest of drawers,
00:01:24.875 --> 00:01:27.745
if you want to know
what's inside the first drawer,
00:01:27.745 --> 00:01:29.775
you would just open it up and look inside.
00:01:29.775 --> 00:01:31.950
So how do we do that with our array?
00:01:31.950 --> 00:01:34.372
Well, we can just type
the name of the array,
00:01:34.372 --> 00:01:36.102
and then the brackets again--
00:01:36.102 --> 00:01:37.542
ooh, I misspelled it--
00:01:39.432 --> 00:01:42.652
and then the number
of whatever it is in the array, right?
00:01:42.652 --> 00:01:45.459
So maybe it would be 1. Okay?
00:01:45.459 --> 00:01:46.669
So, let's actually try this out
00:01:46.669 --> 00:01:50.869
by using the text command
and showing Sofia on the canvas.
00:01:50.869 --> 00:01:55.125
So we say, myFriends--
ooh, friend is a hard word to spell, huh?
00:01:55.125 --> 00:01:59.565
So, myFriends[1],
and then we put it here.
00:01:59.565 --> 00:02:01.875
Oh, and then let's put a little fill.
00:02:01.875 --> 00:02:06.266
Oh, okay, so we see John.
Why do we see John?
00:02:06.266 --> 00:02:09.367
We said 1 for the element index, right?
00:02:09.367 --> 00:02:11.177
The 1 right here.
00:02:11.177 --> 00:02:15.513
Well, that's because
arrays start at 0, not 1,
00:02:15.513 --> 00:02:19.833
and it'll definitely seem weird at first,
but you'll get used to it.
00:02:19.833 --> 00:02:24.075
So if we put 0,
then we see Sofia, all right?
00:02:24.075 --> 00:02:26.125
And then, if we want to show
the next element,
00:02:26.125 --> 00:02:27.754
then we use 1, all right?
00:02:27.754 --> 00:02:30.156
So, and then if we want to do
the final element,
00:02:30.156 --> 00:02:32.466
the third element, then we use 2.
00:02:33.046 --> 00:02:36.616
So you just think to yourself:
"Okay, which one do I want to retrieve?"--
00:02:36.616 --> 00:02:38.267
Oh, let's spread these out--
00:02:38.267 --> 00:02:41.298
and where is it located,
and you just subtract one.
00:02:41.298 --> 00:02:43.742
So the first one is 0,
the second one is 1,
00:02:43.742 --> 00:02:46.902
the third one is 2, et cetera, et cetera.
00:02:46.902 --> 00:02:51.927
What happens if I forgot,
and I try to access Leif this way?
00:02:51.927 --> 00:02:55.920
Well then, we say myFriends[3],
and we'll just get nothing.
00:02:55.920 --> 00:02:58.490
That's because there's nothing there, right?
00:02:58.490 --> 00:03:02.149
When it says 3,
it looks for the fourth element,
00:03:02.149 --> 00:03:05.432
and there's no fourth element,
so there's just nothing.
00:03:05.432 --> 00:03:07.647
And that's something that can happen a lot
when you're using arrays,
00:03:07.647 --> 00:03:09.697
so just look out for that.
00:03:09.697 --> 00:03:12.035
And the same thing
if I tried to access a hundred
00:03:12.035 --> 00:03:15.426
because I don't have a hundred friends,
I only have three,
00:03:15.426 --> 00:03:19.132
so then we get nothing, all right?
So let's get rid of those.
00:03:19.132 --> 00:03:21.771
Now, let's say we want to keep track
of how many friends we have
00:03:21.771 --> 00:03:23.981
because I'm really proud,
and I have three friends,
00:03:23.981 --> 00:03:25.721
and I want to let everybody know.
00:03:25.721 --> 00:03:28.693
So I'm going to go
and declare this to the world.
00:03:28.693 --> 00:03:32.508
So, "I have " + numFriends + " friends!!!".
00:03:32.898 --> 00:03:34.398
Woo, all right.
00:03:36.668 --> 00:03:40.348
Okay, so I have three friends. Yay!
Oh, that's not very many.
00:03:40.348 --> 00:03:43.989
Okay, so maybe Winston feels bad for me
and says he'll be my friend,
00:03:43.989 --> 00:03:45.679
and he says I can add him to the array.
00:03:45.679 --> 00:03:47.375
And I was like:
"Okay, cool. Thanks, Winston."
00:03:47.375 --> 00:03:48.835
So I add Winston.
00:03:48.835 --> 00:03:50.958
Oh, but it still says
I have three friends, right,
00:03:50.958 --> 00:03:53.929
because I have to go
and update this variable here.
00:03:53.929 --> 00:03:56.736
That means, every time
that I add something to this array,
00:03:56.736 --> 00:03:59.796
I have to update this variable,
and that could get really annoying,
00:03:59.796 --> 00:04:01.436
especially if all of you guys
watching this
00:04:01.436 --> 00:04:03.430
decide you'll be my friend.
00:04:03.430 --> 00:04:05.330
And then, you know,
I'm updating this thousands of times
00:04:05.330 --> 00:04:07.640
and having to update this every time.
00:04:07.640 --> 00:04:09.105
So here's the thing:
00:04:09.105 --> 00:04:13.675
We, so often, want to know
how long our array is,
00:04:13.675 --> 00:04:15.755
that there's a special way to do that.
00:04:15.755 --> 00:04:18.140
So the array will keep track
of how long it is
00:04:18.140 --> 00:04:20.090
using a property called length.
00:04:20.090 --> 00:04:25.365
And to use it, we just say,
myFriends.length,
00:04:25.365 --> 00:04:26.755
and then we'll get back the length.
00:04:26.755 --> 00:04:29.115
See, now it says 4,
and I can delete this variable.
00:04:29.115 --> 00:04:30.528
I don't need it any more.
00:04:30.528 --> 00:04:33.024
And this property will update
whenever we add.
00:04:33.024 --> 00:04:35.258
So maybe OhNoes Guy!!
says he'll be my friend, and I'm like:
00:04:35.258 --> 00:04:39.468
"Okay, you're kind of mean,
but okay, you'll be my friend."
00:04:40.168 --> 00:04:42.817
And we can keep adding
and it'll keep updating.
00:04:42.817 --> 00:04:46.600
So, that's really cool because, you know,
00:04:46.600 --> 00:04:49.600
it's a lot less work to keep track
of how long our array is.
00:04:49.600 --> 00:04:50.861
All right, so pretty much,
00:04:50.861 --> 00:04:53.541
whenever you want to store
a list of values like this,
00:04:53.541 --> 00:04:54.921
we'll use an array.
00:04:55.571 --> 00:04:57.499
So keep watching to find out
all the really cool things
00:04:57.499 --> 00:04:59.088
that we can use them for.