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

Same results but with less code (my solution)

I got the same results from "student_report.js" but with a FOR IN loop. Any comments/suggestions on my approach?

var html = '';
var student;

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

for (var key in students) {
  student = students[key]
  html += '<h1>Student: ' + student.name + '</h1>';
  html += '<p>Track: ' + student.track + '</p>';
  html += '<p>Achievements: ' + student.achievements + '</p>';
  html += '<p>Points: ' + student.points + '</p>';
}

print(html);

Looks good to me, nice job :)

Isaac Minogue
Isaac Minogue
3,543 Points

Mine was also quite similar! Perhaps not as good as yours, as I keep referring to 'key' instead of stating it once as you did. However, it is interesting that in this challenge the 'for' loop works in a similar way to the 'for in.'

for (var key in students) {
  message +=  '<h2>Student: ' + students[key].name + '</h2>'
  message +=  '<p>Track: ' + students[key].track + '</p>'
  message +=  '<p>Points: ' + students[key].points + '</p>'
  message +=  '<p>Achievements: ' + students[key].achievements + '</p>'
}

Does anyone know the benefits and disadvantages of either way?

1 Answer

I kept mine pretty quick, simple, and dirty:

var html = ''; for(var prop in student){ html += '<h3>Student: ' + student[prop].name + '</h3>'; html += '<p>Track: ' + student[prop].track + '</p>'; html += '<p>Points: ' + student[prop].points + '</p>'; html += '<p>Achievements: ' + student[prop].achievements + '</p>'; } print(html);