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

Tommy Gebru
Tommy Gebru
30,164 Points

Bug?

A single line of code passes when following directions however it asks for each. So when I apply more than one line of code the challenge does not pass!

Challenge 1 of 2 Use a for in loop to log each of the property names of the shanghai object to the console.

script.js
var shanghai = {
  population: 14.35e6,
  longitude: '31.2000 N',
  latitude: '121.5000 E',
  country: 'CHN'
};
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

Can you show me what you've tried? I have a working solution and I want to see if it's comparable to yours.

Tommy Gebru
Tommy Gebru
30,164 Points
for( var population in shanghai ) {console.log(population);}

this works fine, however the challenge ask that we do this for all props, but only one line of code works for me. not 2,3, or 4 passes.

I think technically that code should run, but the code challenge is stopping you because it sees that you aren't accessing the value of each property.

At the end of the challenge, it asks you to format your output like this: "population: 1435e6"

With what you have right now, if it worked, it would print out only the property names.

Let me know if it works for you, I can give you the solution that works for me if you would like.

Tommy Gebru
Tommy Gebru
30,164 Points

So the same issue applies to the 2nd Coding Challenge. I think that the Instructions should be rewritten to let students know to pass one line of code.

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

Well your code is indeed correct, it's strange that it won't allow you to write more lines. Maybe it is a bug.

For reference, I wrote this and it passed -

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

and the step one equivalent worked as well. Maybe copy that code over and see if it confirms that there is a bug on your end.

Tommy Gebru
Tommy Gebru
30,164 Points

I have played around with it as well, I wanted to check if others have met the same problem.