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

Zaid Anki
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Zaid Anki
Full Stack JavaScript Techdegree Graduate 14,514 Points

for in loop for this challenge?

instead of doing all of what the video has done, could i just do this instead? or am i missing something?

for(var key in students){
  document.write('<p><b>' + 'name' + ': ' +students[key].name + '</b></p>')
  document.write('<p>' + 'track' + ': ' +students[key].track + '</p>')
  document.write('<p>' + 'achievments' + ': ' +students[key].achievements + '</p>')
  document.write('<p>' + 'points' + ': ' +students[key].points + '</p><br>')
}

1 Answer

Steven Parker
Steven Parker
231,275 Points

That would certainly be close to the video example, and the "for..in" loop is a more compact and completely equivalent choice. But the differences include:

  • the video code updates the "output" div, whever it is in the document
    but this code only adds on to the end of the document
  • the video code uses the "student" variable to avoid needing to apply the same index over and over
  • the video separates the output method and the formatting in separate functdions to simplify maintenance