Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects Accessing All of the Properties in an Object

Chris Carr
seal-mask
.a{fill-rule:evenodd;}techdegree
Chris Carr
Front End Web Development Techdegree Student 12,900 Points

task 1 is no longer passing

What am I doing wrong?

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);
  console.log(shanghai[prop]);
}
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

Shawn Flanigan
PLUS
Shawn Flanigan
Courses Plus Student 15,803 Points

Hi Chris,

You're almost there, but the challenge asks you to string 2 pieces of information together for each log statement (the name of the property and it's value...separated by a colon). The way you've written your code, you're sending 8 lines to the log, like this:

population
14.35e6
longitude
31.2000 N
latitude
121.5000 E
country
CHN

Instead, you'll want to string your outputs together to send just 4 lines to the log. Remember, to concatenate strings and variables together, you'll use a +. Hope that helps, but let me know if you need a little more clarification on how to get the desired output.

Chris Carr
seal-mask
.a{fill-rule:evenodd;}techdegree
Chris Carr
Front End Web Development Techdegree Student 12,900 Points

This passes task one but still does not work.

for ( var prop in shanghai) {
 console.log(shanghai.prop + shanghai[prop]);
}

and this outputs task one no longer passing

var property = '';
var value = ''; 

for ( var prop in shanghai) {
  property = shanghai.prop; 
  value = shanghai[prop];
  console.log(property + value);
}
Shawn Flanigan
Shawn Flanigan
Courses Plus Student 15,803 Points

Chris,

Let's go back to the way you were solving the problem before. Here's the for loop you had originally:

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

You were close here, you just needed to combine your 2 log statements into one. That would look something like this:

for (var prop in shanghai) {
console.log(prop + shanghai[prop]);
}

This will give you 4 lines in your log that look something like:

population14.35e6
longitude31.2000 N
latitude121.5000 E
countryCHN 

Now, to clean it up and make it readable, they ask us to add a colon and a space in the middle of each log line. So...we'll add a string literal into your console.log command and you end up with something like:

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

This should give you the desired output.