Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects Accessing All of the Properties in an Object

What am I doing wrong here please?

I am trying to complete the following coding challenge:

Now that you are logging out the property names, include the property values too. In other words you want to log out 4 lines that include both the property name and value. For example: "population: 14.35e6"

My code is attached to the post. I keep getting the following error:

"Bummer: The console property should only be called four times: once for each property."

As you can see, I have only called it 4 times to relate to the four fields. I have tried literally everything I can think of to get this to work but no luck so my final idea was to post this here.

I would appreciate it if anyone could help me on this so I can continue with the course.

Thanks in advance!

script.js
var shanghai = {
  population: 14.35e6,
  longitude: '31.2000 N',
  latitude: '121.5000 E',
  country: 'CHN'
};

for (var prop in shanghai) {
  console.log(prop, ': ', shanghai["population"]);
  console.log(prop, ': ', shanghai["longitude"]);
  console.log(prop, ': ', shanghai["latitude"]);
  console.log(prop, ': ', shanghai["country"]);
}
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Objects</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

1 Answer

Steven Parker
Steven Parker
231,268 Points

You only wrote 4 lines in the loop; but since the loop runs once for each property, it will log 16 lines.

You can easily log each property separately:

for (var prop in shanghai) {
  console.log(prop, ': ', shanghai[prop]);
}

Thanks for the quick reply, that worked! I've marked this as best answer so I hope this can help others too.