So a little while ago I had a 24 party, and I invited some people, and those people invited 1
some people, and before you know it, 2
I've got a massive guest list to manage. 3
So what's the obvious solution? 4
Well, code, of course. We’re programmers, we have all the codes. So I thought, what if I could write a script 5
that asked the guests for their name and checks against a guest list to see whether or not they should 6
be admitted? 7
Now the actual implementation was done via Alexa, which is a little bit hairy, but let's see if we can 8
make a basic version of this using Javascript. 9
Now using what we know about Javascript so far, we could write maybe like a gigantic conditional statement, 10
say if guest name equals Angela then alert("Welcome!), 11
else if guest name equals Jack then alert("Welcome!"), and so on and so forth until I basically break my 12
fingers or I run out of motivation. 13
So the problem here is that we've seen that you can store data in variables but we've only been able 14
to store a single piece of data, 15
so for example var a equals Angela. 16
And we've seen that you can store different data types, but how can we store a collection of related 17
items into the same container or the same variable? 18
Well, this is a case for creating an array. 19
So let's say that we have a single piece of data, an egg 20
in this case. Instead of storing a single egg into a variable, we can also store it inside an array, or, 21
say, an egg box full of eggs. 22
And this is essentially what an array is. 23
It's a collection of items that 24
are related, and they can be stored together into the same container or the same variable. 25
And this is what the syntax looks like. 26
So you would create a variable, 27
give it a name, 28
let's say call it eggs, and we can set it to equal a bunch of data and enclose it inside these square 29
brackets, with commas in between each piece of data. 30
And, if we try to retrieve a piece of data from this array, then we can simply use the name of the array, 31
eggs, and specify the position of the data inside the array by using, again, square brackets. 32
Now just take a quick guess. Which egg 33
do you think my egg will equal if I run these lines of code, 34
given that the position that I'm trying to tap into is the egg at position 1 inside the eggs array? 35
So if you remember from before we said that computers always start counting from zero. 36
So as programmers we also always start counting from zero. 37
So the first egg in the array is in fact at position zero, 38
and that means that the second egg is in fact the one at position 1, which is a little bit weird, 39
but this is the egg that we would get and we would store inside that variable 40
myEgg, 41
if we ran the code that said var myEgg = eggs 42
[1]. 43
Now we can other things with arrays as well. 44
We can say eggs.length, 45
and this counts the eggs inside the eggs array and gives us the number 5 back, because there are 5 items 46
inside the array. 47
So using arrays we can create a very simple guest list. So we can say something like, for example, var 48
guestList = [], and it's really important to remember that whenever 49
you see square brackets, then it's usually something to do with a collection data type such as an array, 50
and we use these square brackets when we're creating arrays as well as when we're trying to retrieve items 51
from arrays. 52
So this is a telltale sign that we're dealing with something that holds a number of data items such 53
as an array. 54
So inside our guest list I'm going to add all of these names that are on the guest list. 55
So let's turn these names into strings and now I can add them one by one into the guest list remembering 56
to add a comma in between each piece of data. 57
So here's our brand new array that is stored inside a variable called guestList. And this array contains 58
all the names of the people who are on my guest list. So you can see that if I use console.log and 59
I try to print what's inside my variable guest list then it'll print out all of the items inside my array 60
like so, and it will also tell me that there are 6 items inside this array. 61
Now we can also find this out by simply saying 62
guestList.length, and this will do exactly the same thing, 63
it will count how many pieces of data are currently inside the guest list and give you a output of that 64
number. 65
Now let's see if we can retrieve the first guest on that guest list. 66
So, remember, when we want to retrieve items from the array, we have to start counting from 0. And we retrieve 67
items in the same way that we create items, using the square brackets, and inside the square brackets 68
we specify the position of the data that we want to retrieve. 69
So let's say that if I want the first piece of data, then I would say guestList [0]. And this, 70
if I console.log it, 71
you can see is Angela, 72
which we can confirm is the first piece of data inside our array. 73
Now another cool thing that you can do with arrays is that you can use the function includes to find 74
out if an array includes a particular item. 75
So, for example, in this case, if I say eggs.includes this specific egg, then the computer will look through 76
your array and see if it matches with any of the items inside the array. 77
And if it does then this line of code will return true, and if it doesn't then it will return false. 78
So we get a boolean as the output of this function. 79
So, given what you've just learnt, can you write some code that creates a prompt that asks the guest what 80
is their name, which they will then type into the prompt, 81
then you will check their name against all of the names inside our guest list, 82
and if it does exist then it will send an alert saying "Welcome!", and if it doesn't exist we'll tell 83
them "Sorry, 84
maybe next time.". So, I'll go through the solution with you shortly after, but pause the video now and try 85
and give that a go. 86
You might need to rewind the video back and review some of the concepts that we spoke about in this 87
lesson. 88
All right. So the first thing we're going to do is we're going to create a variable called guestName, 89
and this will be set to equal the response that we get back from the user when we ask them 90
"What is your name?". 91
And now we can check to see if the guest list includes the guest name by using that includes function 92
that we spoke about earlier on, and this will return either true or false depending on whether if the 93
guest name is included inside the guest list. 94
So let's run our code and see what we get. 95
So I'll give it a name that I know is definitely on the guest list, so my own. 96
I'm going to hit 97
OK. Now that name is going to be stored inside guestName, and our next line of code on line 6 is going 98
to check inside the guest list, 99
is there a piece of data that is equal to the guest name, and we get back 100
true, so in fact the guest list does include the guest name. But say if we put in some random name or 101
some random characters, then we get false 102
once this line of code is evaluated. 103
So how can we use this to give the user a different alert based on this condition? 104
Yep, you guessed it, we have to use conditionals. So if we use this as the condition, then we can create 105
an if statement that sends an alert which tells the user "Welcome!", and if the condition was false then 106
we'd give them an alert that says "Sorry, maybe next time.". 107
So now, if we run our code, we can see if I give it a name that it recognizes, 108
it'll give me an alert saying "Welcome!", and if I give it a name that it doesn't recognize, then it'll say 109
"Sorry, maybe next time.". 110
So that's pretty simple, right? 111
So this is how you can store a collection of data inside an array, 112
and it gives you a lot of functionality without having to write a lot of code. 113
Now, in the next lesson, we're going to explore how you can use arrays on a more intermediate level. 114
But before I leave, here's a question. Why did the programmer quit his job? Because he didn't get arrays. 115
I'd just like to say at this point that, in this course, all the jokes that you think are funny I will take 116
credit for, and all the other jokes, 117
it's down to the Internet. 118
So I'll see you on the next lesson.