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 The Build an Object Challenge, Part 2 Solution

Md. Syful Islam
Md. Syful Islam
9,463 Points

The Treehouse solution is great but I tried to produce the output dynamically!

The solution given by Dave where he hardcoded the property names. But my attempt was for producing the result dynamically. My for loop was as following:

for (let i = 0; i < students.length; i++) {
  for (let key in students[i]) {
    let propName = key;
    let propValue = students[i][key];
    message += `<h2>${propName} : ${propValue} </h2>`;
  };
};

Though it produces the same result, it doesn't look exactly as Dave because of the HTML output.

Would appreciate if anyone review my code. Cheers!

1 Answer

Blake Larson
Blake Larson
13,014 Points

Yes that works! You can also clean it out a little more because you already have the key and specific student in each iteration of the for/in loop so you don't have to set the variables. Setting the propValue variable would be a good idea if you are using students[i][key] multiple times in the code block to make it cleaner but this is a one line block.

for (let i = 0; i < students.length; i++) {
  for (let key in students[i]) {
    message += `<h2>${key} : ${students[i][key]} </h2>`;
  };
};
Md. Syful Islam
Md. Syful Islam
9,463 Points

Yeah right! The code can be cleaner this way. Thanks for your reply. Appreciate it!