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

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,815 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,815 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.