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 trialshiny peanut
5,113 Pointsfor in loop, javascript
Usually I have some idea of what's going wrong, but not so much here. The video didn't really clear it up. I'm not really getting how two variables can go through this one loop.
var shanghai = {
population: 14.35e6,
longitude: '31.2000 N',
latitude: '121.5000 E',
country: 'CHN'
};
for (var property in shanghai) {
console.log(property, ', 'population);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Objects</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
2 Answers
Michael Hulet
47,913 PointsThere won't really be 2 variables going through this loop. With the code you've written, the loop will go over each property in shanghai
and set the variable you declared called property
to the key that the loop is currently on. With the first iteration, property
will be "population"
, and it'll be "longitude"
on the 2nd, "latitude"
, on the 3rd, and finally "country"
on the last. Remember that you can access an object's property via subscript syntax, like this:
const myObject = {someProperty: 5};
console.log(myObject["someProperty"]); // This will log the number 5
Keeping those things in mind you should have the tools to pass the challenge. You're doing great, and you're almost there!
shiny peanut
5,113 PointsI'm finished with the course except for this question. I thought that going through more of the videos would help me put it together, but I just don't get it. At all. It keeps telling me that I'm passing the property value and not the name no matter what I try.
Michael Hulet
47,913 PointsThat's ok, we're all still happy to help. Struggling a bit is totally normal. Just so I know where you are, is the latest code you've tried still the same as in your question? If not, I'd love to see where you are now