This course has been updated! We now recommend you take the JavaScript: From First Steps to Professional course.

Check out a free preview of the full JavaScript: From Fundamentals to Functional JS, v2 course:
The "Game Characters Solution" Lesson is part of the full, JavaScript: From Fundamentals to Functional JS, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Bianca walks through the solution with the help of students.

Get Unlimited Access Now

Transcript from the "Game Characters Solution" Lesson

>> So we're just back from our exercise. And the exercise was to create an object using bracket and dot notation that represents the characters and whatever related data you may find in a game of Clue. So what's the first thing I need to do? Who wants to go first, how about someone in the middle?

[00:00:21] Anyone wanna volunteer themselves? The very first thing is always the easiest one. Okay, what's the very first thing?
>> You have to make an empty object.
>> Yep. But what's the first thing you need to type?
>> Var.
>> Okay, and we initialize that as a object literal, put a semicolon in there.

[00:00:45] All right, what's the very next thing that we should do?
>> Define a property. So do game.murderer="??"
>> I like it, the mystery. What's the next thing? Flannel shirt.
>> Another property for weapons.
>> Weapons, how do we do that without that notation? Like this?
>> You could do it with a string.

>> Yeah. Okay.
>> I made an array of-
>> Awesome.
>> Of gun and-
>> Stuff like that?
>> Tank.
>> All right, lazer beams. Oops, that's not how you spell laser. Lasers, angry cats. All right. Should I put Monster energy drink in here?
>> [LAUGH]
>> Awesome, so this is cool.

[00:02:10] Why would we choose an array versus an object in this case? We haven't talked too much about when we would choose one or the other. I'm assuming that you guys have all used objects and arrays and have had to make that decision over which data structure you're gonna use.

>> Is it just easier to store a lot more data inside of an array?
>> The size doesn't really matter. It's more about the structure of the data here since this is a collection of things that are the same essentially and they don't have. Yeah, basically it's because it's a collection of things that are same, they're all weapons so it's just a list of weapons.

[00:02:57] Versus an object is more like a document you can think of it, you can make bigger things with it and have more properties. But you can also make an array of objects and things like that, so you can combine them. So if I was gonna make an array of objects here, How might I do that?

[00:03:22] Let me just break this out into lines here, Just so we have space. So an object has a key and a value, right? So what could be the key here?
>> Weapon one, weapon two.
>> Yeah, well the thing is we have indexes and so we can line up anything that has a number like that we'll just refer to as the index.

[00:03:53] So weapon one or zero would be lasers, etc. Two would be dish soap, very deadly. So what would be a property name? You can call a property name also a key, just so you know it's the same thing. Property name. All right, how about, who is next in the list?

[00:04:26] How about a friend in the way back?
>> Well you have to have say maybe lasers and then the location that the lasers are at. So you would do the lasers, colon, and then the string of like found in the lab.
>> Hm, okay, that's interesting. You could do that, that's valid.

[00:04:51] But I would recommend having a property name that is consistent among your objects. So for example, I would say to do maybe type: 'Lasers' and then location: 'Lab'. Do you guys see the difference there? Because now each of these data structures has a type, it has a location, and we can organize our data around that, versus having to know what the property name is for a specific thing every time.

[00:05:24] So if the property name was lasers and here it was angry cats, we may have to remember that. Whereas we could just say weapon set type and it returns lasers, awesome, we don't have to remember the specific string that's stored there. And this is a pattern. Etc., cool.

[00:05:50] What else might we have in our game? You guys know the rest of this, right, would be the same pattern.
>> Maybe character's name?
>> Character's name?
>> Of the game.
>> The name of the game? Name the game.
>> No, like Mr. White [CROSSTALK]
>> The characters.

[00:06:14] How would I do that?
>> The way I did I did, I guess it would be and then I pass an array with different nicknames saved as a string.
>> Cool. So someone know a name of a character of Clue?
>> Miss Scarlet.
>> 'Miss Scarlet'. All right and brown sweater, I'm sorry, I don't know your names.

>> Mrs. Peacock.
>> Well actually my question is a little bit different, sorry.
>> [LAUGH]
>> How will we add Miss Scarlet to that array, Without just putting it in there an array literal? So this is array literal where we just pop it in there, but we wanna add it later or add it dynamically.

>> You'd do a bracket notation, or just [CROSSTALK]
>> Sure, tell me exactly what to type. Pretend like I'm, what's that, Dragon dictation software?
>> I don't know. But,

>> Oops.
>> Equals.
>> Nice. Perfect.
>> Semicolon if you want it.
>> I do want it. Is anyone here anti-semicolon?

[00:07:40] Okay, just wondering if we could still be friends. Just kidding, I don't engage in those silly things. But I do use semicolons. Awesome.
>> Or push.
>> Yeah, you can use push. What does push do?
>> In the green sweatshirt
>> Puts it at the front of the array.

>> Puts it at the back of the array. But, yeah. So then an array would look something like this, Miss Scarlet and then Mr. Green, or does it? Can someone tell me what's wrong with this? How about black sweatshirt.
>> Me? Mm-hm.
>> What's wrong with the array?

>> Mm-hm. Or what's wrong, so we just added, we used the push method.
>> Mm-hm.
>> And I was saying we want it to look like this, and now I'm hinting that maybe there's something wrong.
>> You already have Mr. Green there [CROSSTALK]
>> Well, this is not real.

[00:08:54] This is what we want, and I'm hinting that maybe this line is wrong. How did we add Miss Scarlet before?
>> Used a bracket.
>> Mm-hm, but what's the difference between line 14 and 15?
>> I blank out when I'm put on the spot. [LAUGH]
>> Yeah, it's okay.

>> You have the half circles there [CROSSTALK]
>> Yeah, so this is a method. Absolutely, but what about this part?
>> The name property.
>> Yeah, so we're pushing it to the game which is an object. So we can't push things to an object, we can only push things to an array.

[00:09:36] So what we need to get to that point was we just need to reference the next nested property. Cool, so now we're gonna move on unless we have any questions to nested properties. What's up?
>> Just a little circle back from the previous before the break. If we were to assign game as a array, so then it would still work as an array of objects?

>> So you're saying if we just did this?
>> Yeah.
>> Yeah, everything would work exactly the same.