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

Isaiah Gadson
Isaiah Gadson
6,398 Points

challenge task 2 of 2 now that you're logging the property names, include the property values too. the console should di

i type for ( let property in student) { console.log( ${instrument}: ${instrument[piano]} ); } and it's not working i

script.js
const composer = {
  name: 'Edward Ellington',
  nickname: 'Duke',
  genres: ['jazz', 'swing'],
  instrument: 'piano'
};
for (let property in student) {
  console.log( `${instrument}: ${instrument[piano]}` );
}

2 Answers

Cameron Childres
Cameron Childres
11,820 Points

Hi Isaiah,

Currently your for/in loop contains "property" and "student", but you aren't using them. The first thing to change is "student" to "composer" so that you're accessing the object written in the code:

for (let property in composer) {

}

Written this way the loop will iterate over every key in "composer", represented by "property". To log each of those to the console you would use "property":

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

// This prints the following to the console:
// name
// nickname
// genres
// instrument

From here we have a good jumping off point for the second part of the challenge. The goal is to print a line with each of these keys followed by a colon and their value. You can get the value with bracket notation by combining the name of the object with the key that you want the value for. If I wanted 'Duke' then I would type composer['nickname']

Since we're already looping through all of those keys we don't need to explicitly type any of them -- "property" in the loop takes care of that. To access the value for each iteration of the loop you'll type composer[property]. Putting it all together in a template literal will produce the string the challenge is after:

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

// This prints the following to the console:
// name: Edward Ellington
// nickname: Duke
// genres: jazz,swing
// instrument: piano

For a refresher I'd recommend reviewing the previous video in the course: Use 'for in' to Loop Through an Object's Properties. It goes through an example exactly like the one presented in this challenge.

I hope this helps clear some things up! Let me know if you have any questions.

Isaiah Gadson
Isaiah Gadson
6,398 Points

it works! thanks for your help.