All right.
So it's time for another quick set of exercises.
This time we're going to cover javascript objects.
Just the basics.
You know the drill by now.
I'm going to give you some code and ask you to evaluate it without just copying it into the console
.
So here's the first one we are declaring an object and then we're adding four different properties to
that object.
And I want you to figure out which of these are valid code and which will cause problems.
So go ahead post a video and give it a shot.
OK so let's discuss the answer here.
If we go ahead and create a new object some object equals empty object the first thing that we're doing
is trying to add in Hedwig under the key underscore name.
So does it work to do that.
The answer is yes.
If I look at some object you'll see that we have Headrick stored under underscore name.
It's a little bit unorthodox to see that but it's definitely OK.
So the next one here this is also valid some object dot age you can see get some object it has underscored
name Hedwig.
Age 6.
Next up we're using a variable called Propp which is equal to the string color.
And then we're using Propp as the key inside of square brackets and setting the value to be read.
So if we copy this over and we look at some object you'll see that we get color Colan red which is valid
.
Lastly some objects dot one two three.
Setting that to be true that's problematic.
We can't use the dot notation if we have a property starting with a number.
So that's the only one that's an issue.
Although again using underscore is not a great idea but it doesn't cause a problem.
So here's the second exercise.
Have a large nested object and I want you to write code to retrieve the string Malfoy right here from
some object.
And remember to go one layer at a time.
So go ahead post the video and try and write the code to retrieve Malfoy.
OK so the answer here is that we first need to access the friends array and to do that it's some object
friends and then we need to access the first object instead of the friends array which is right here
.
And then we need to access the name property out of that array.
So let's type that solution.
I'm going to just copy some object over and we're going to need to start by accessing the friends array
.
So Dot friends or we could use square brackets.
That gives us an array with three different people each one with a name.
We want the first one.
So some object friends bracket zero.
That gives us an object with name equal to Malfoy.
And then we want dot name.
And again we could use square brackets as well.
So that's all we need to do there.
The key to answering these is to go one step at a time burrow down or drill down to friends and then
to the first object and then get name.
All right so that's it for this quick set of exercises.