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