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
180,034 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.