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

Robert Fish
Robert Fish
1,032 Points

I need help with this challenge

I have used this below code to log the property name and value of each property in this object and it still won't work

script.js
var shanghai = {
  population: 14.35e6,
  longitude: '31.2000 N',
  latitude: '121.5000 E',
  country: 'CHN'
};
for(var stuff in shanghai){
  console.log(shanghai.stuff + shanghai[stuff]);

}
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Objects</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

2 Answers

Ari Misha
Ari Misha
19,323 Points

Hiya Robert! I wanna point out that shanghai.stuff and shanghai["stuff"] both outputs the same result, values of properties in the object literals. In task one, you're supposed to log out only properties and in the latter, you need to log the a formatted string which uses shanghai object's property and its values. There is one example already shown in the challenge itself. Here is my code for reference:

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

for (var props in shanghai) {
  console.log(props + ": " + shanghai[props]);
}
Raffael Dettling
Raffael Dettling
32,999 Points

ItΒ΄s the same in the video

console.log(stuff, ":",shanghai[stuff]);