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

Can someone please help me with task 2 of Accessing All of the Properties in an Object? I am missing something!!!

I wrote the code below but it keeps saying now Task 1 is no longer correct.

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

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);
}
for(var prop in shanghai) {
  console.log(prop, ': '  shanghai[population]),
  console.log(prop, ': ', shanghai[longitute]),
  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

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there!

When you see that it says task 1 is no longer passing it generally means that there's a syntax error. In your case, it's the second for loop. Keep in mind, before you read this code that you've already figured out the property/key name. So we're going to use that to access the value at that key that has the same value as that variable. Let me see if I can clarify that a bit. You look through every property, right? The first property is population. So now that you have population stored as prop here, you could access the value by doing shanghai[prop]. Take a look at my solution.

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

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

Jennifer, thanks for the fast reply. I am still having issues where it is stating my first task is no longer passing. Do you have any other suggestions?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Robert Yann ... I really wish I knew what to tell you. When I open the challenge (which you can do by right clicking the view challenge button and right click on open in new tab), and then clear the code in script.js, and then copy/pasted the code I posted above... I can simply run the check work button twice and it passes both steps.