#10 list ALL the properties


#1

So i used this code...

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

// write a for-in loop to print the value of nyc's properties

for( var fullName in nyc) {
console.log(nyc[fullName]);
}
for(var mayor in nyc) {
console.log(nyc[mayor]);
}
for(var mayor in nyc) {
console.log(nyc[mayor]);
}
for(var boroughs in nyc) {
console.log(nyc[mayor]);
}

...and the console printed out..

New York City
Bill de Blasio
8000000
5
New York City
Bill de Blasio
8000000
5
New York City
Bill de Blasio
8000000
5
5
5
5
5

why all the 5s? and why did it loop it over and over again? it all went through but it doesn't look right to me. HELP


#2

isn't the point of a loop, that you can go through all the property's in your object with one loop? Why so many loops?


#3

i mean, why does it loop 5 over and over again and not loop the first property again?


#4

because your for loop in is:

for(var boroughs in nyc)

but then you use mayor inside the loop:

console.log(nyc[mayor]);

but mayor is still from your previous loop, which seems to be pointing the last property of your object


#5

OH MY GOD. i didnt even see that. I feel so dumb Dx thank you!