10/30 using a for/in loop to print the VALUES of an object


#1

Objects II: question 10.
I've tried the syntax they gave:

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 prop in nyc {
    console.log (nyc[prop]);
}

this logs an "unexpected identifier". I realize that the bracket notation which relies on the propertiesneeds to come in somehow and perhaps I need to define a separate variable that plugs in to the for/in loop but i have no variable for all the properties in the nyc object... I have to use the for/in loop to return this. The hint they gave is so obvious, but that's exactly what i used. Totally stumped.


#2

Hi there,

The bit you've missed from your for / in loop which is causing this issue is related to the syntax being used.

A for in loop takes the following syntax

for (variable in object) {...}

You have:

for variable in object {...}

Throw some brackets around variable in object and you'll find that it starts to work.

Effectively you'll end up with:

for (prop in nyc) {...}

Hope that helps.


#3

Thank you so much for taking the time to help me solve such a silly error! I really appreciate it.