Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!

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"]);
<!DOCTYPE html>
  <meta charset="utf-8">
  <title>JavaScript Objects</title>
<script src="script.js"></script>

1 Answer

Steven Parker
Steven Parker
216,810 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.