9. Getting IN-timate


#1

var nyc = {
fullName: "New York City",
mayor: "Bill de Blasio",
population: 8000000,
boroughs: 5
};

for (var property in nyc) {
console.log(nyc[property]);
}

I followed the instructions, and I wound up with this code. I am able to pass the lesson, but I don't completely understand my code and why I passed it. Thank you to anyone who helps me!


#2

Hi try to change your console.log like that console.log(property);


#3

I found my problem. Thank you for helping me! @wizmarco


#4

Ok, first you made an object called nyc. It has some properties and they've got some values. Regular behavior here.
Then you make for loop (or foreach, because you loop through each property inside nyc).
So, what you get as property are [fullName, mayor, population, boroughs]. I've put them to array because they are multiple, but you only get one at a time. So in first looping you have fullName.
You call console.log(), and try to print nyc[property]. Property is now fullName, and since fullName = "New York City" in your nyc object, it's printed. Second looping you have mayor. When you're printing it, it will again check if it exist. Since it does exit, it prints Bill de Blasio.

This will continue for as many properties you have on that object.

EDIT: console.log(property)will just give you property name. Since you stated in your original post that you don't understand the code, but passed the lesson, I hope I managed to help you a bit.