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

Mavis Lok
Mavis Lok
3,440 Points

Doubtful of my code although it works

After watching the teacher's methods and reading everyone else's codes/questions, I'm starting to doubt my code despite it working.

for ( var prop in students ) {

  document.write('<h2>Student: ' + students[prop].name + ' </h2>');
   document.write('<p>Name: ' + students[prop].track + ' </p>');
    document.write('<p>Achievements: ' + students[prop].achievements + ' </p>');
     document.write('<p>Points: ' + students[prop].points + ' </p>');

}

How can I make it better?

2 Answers

Gabriel Plackey
Gabriel Plackey
11,064 Points

What are you doubting about it?

The only thing I would recommend is using/making the print function as I don't think you will really use document.write that much in a practical way. Everything else is the same concept, but using a for in loop instead of the normal kind of loop. There are differences between the two and how they loop but for this purpose they work the same.

Steven Parker
Steven Parker
230,250 Points

What kind of doubts are you having about it?

I don't see any technical issues, but since the loop variable will represent the index of one of the elements of the "students" array, the name "prop" is a peculiar choice for it. A typical convention for a numeric loop index is to name it "i", but that will make no difference to performance.

Another minor readability issue is that it's usually considered a "best practice" to indent all lines that are at the same nesting level by the same amount.