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 Objects Loop Through Objects Loop Through an Object's Properties

Now that you're logging the property names, include the property values too. The console should display four lines that

Could someone please give me the answer

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

4 Answers

rydavim
rydavim
18,813 Points

The code you posted doesn't seem to be from the challenge you've linked to, despite having the same goals. Using your code, let's take a look. You have the right idea, there are just a few issues with your syntax.

Note that the object shanghai is already provided at the top. You should not repeat this code, you only need to write the for...in loop.

// ORIGINAL
for (var prop in shanghai) { // looks good!
  console.log(prop, ': ', shanghai[prop]); // right track, some issues
}

So, your loop is lookin' good! Let's break down the console.log statement.

You should get an error message suggesting you use Template Literals. These allow for embedded expressions in your strings without needed complicated concatenation. Template literals need to be wrapped in backticks (grave accents) - they look like ` and are normally found on the ~ key to the left of the 1 on a standard keyboard.

Template literals contain placeholders indicated by ${} notation. These curly-braces stand in with the variables you're trying to include in the output. So you'll want to wrap what you've got above - ${prop}, for example. The other advantage is that you wrap the whole thing, no + or " ' needed! Check out the code block below for a simple use example.

// Example Syntax
for (var item in items) {
  console.log(`Key: ${item} & Value: ${items[item]}`);
}

Hopefully that helps you get on the right track, but let me know if you still feel stuck or have any follow up questions! Good luck, and happy coding!

var shanghai = { population: 14.35e6, longitude: '31.2000 N', latitude: '121.5000 E', country: 'CHN' }; for (var prop in shanghai) { console.log(Key: ${prop} & Value: ${shanghai[prop]}); } it didn't work

rydavim
rydavim
18,813 Points

The code I posted was just an example. The challenge is likely asking for a different format. I'm wondering if it is somehow region-based, because I don't see anything about Shanghai when I go to that challenge. Assuming the requested wording is the same, you need to ensure your output is exactly what the challenge is asking for.

for (var prop in composer) {
  console.log(`${prop}: ${composer[prop]}`);
}

composer is the object I see in the linked challenge. If yours is shanghai, make sure to update the above to match what they're asking you for. Let me know if that doesn't work for you.

console.log (${prop}: ${composer [prop]}

whats wrong here

I'm also having issues with this challenge...

This is what I have entered, but it's rejecting the answer and I don't know what's wrong. I've tried it with both property and prop.

const composer = { name: 'Edward Ellington', nickname: 'Duke', genres: ['jazz', 'swing'], instrument: 'piano' };

for ( let property in composer ); { console.log(${property}: ${composer[property]}); }

rydavim
rydavim
18,813 Points

Normally it's best to start a new question if you're having problems with your code Michelle Aubrey - this question already has an accepted answer (green), so it will show up as solved on the community board.

You've only got a small typo, though, so I'm gonna go ahead and do a quick comment here. You're just erroneously closing before the loop block.

for ( let property in composer ); { // you've got a closing ; here
console.log(`${property}: ${composer[property]}`);
}

Take that out and you should be good to go, your code is otherwise great. Nice work!

caleb currier
caleb currier
8,265 Points

const composer = { name: 'Edward Ellington', nickname: 'Duke', genres: ['jazz', 'swing'], instrument: 'piano' };

for (let property in composer) { console.log(property); }

for (let property in composer) { console.log(${property}: ${composer[property]}); }

my code above isn't working, can't figure out why. any help would be much appreciated.