Make a Name for Yourself explainer


#23

This is an empty object called book. It has nothing inside of it, but it’s still an object.

var book = {
};

Below, I’ve added a property to the book object. The property has a key and a value: the key is title and the value is 'Basic Economics'.

var book = {
  title: 'Basic Economics'
};

Let’s add another property – the author of the book, Thomas Sowell.

var book = {
  title: 'Basic Economics'
  author: 'Thomas Sowell'
};

Now let’s do something with our book object. If I use print(book), the output is just [object Object]. This isn’t very useful, we already knew the book was an object. What if we just want to print out the author of our book. We use book.author to access the author property of our book object.

print(book.author) – this will print out Thomas Sowell.

To print out the title of the book:

print(book.title)

What if we had another object that looked like this:

var song = {
  title: 'Lift Yourself',
  artist: 'Kanye West'
};

How would you print out the title of the song instead of the title of the book?

print(song.title)

What if you had an array containing a bunch of objects that all had title properties? How could you print out all of them?

for (var element of arrayOfAllTheObjects) {
  print(element.title);
}

What if you had an object that had another object as a property value? For example, a person named Grace that has 2 pets: a dog named Pongo and a cat named Precious. If you were given the object, how would you print out the name of the person’s dog?

var person = {
  name: 'Grace',
  pets: {
    dog: 'Pongo',
    cat: 'Precious'
  }
};

print(person.pets.dog); – Find the person object, get the pets property (which is another object) Then inside that pets object, get the dog property, which gives the value ‘Pongo’.

–Frankie


#24

I have a question about this exercise. Why we need to put ‘last’? I have been writing ‘final’ and I don’t understand why ‘last’ is accepted and ‘final’ no.


#25

Swapping last for final will create a working solution, but the puzzle will not pass. This is because there are endless ways to name your properties or variables. We need to check that the correct properties are being referenced later in your code, and it is simplest to limit the choices so that the “last name” property is used as name.last.

–Frankie