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 trialTom S
11,912 PointsIs this as valid?
I used this code:
for (prop in students) {
document.write('<h2> Name: ' + students[prop].name + '</h2>');
document.write('<p> Track: ' + students[prop].track + '</p>');
document.write('<p> Achievements: ' + students[prop].achievements + '</p>');
document.write('<p> Points: ' + students[prop].points + '</p>');
}
And it produced the same result as Dave's code. I'm assuming that this isn't the right way to go about solving this problem?
1 Answer
Jacob Mishkin
23,118 PointsSameer,
the general rule of thumb is, use for loops to iterate through indices of arrays, and to use a for in loop to iterate through objects. In the example provided you are iterating though the properties of objects, a for in loop is perfect for that. My only issue with your code is using the document.write method. you should create a function that prints to the page by using either innerHTML or innerText.
There is never a wrong way to code something. Think of it more as best practice and logical. Does this code create something that is in accordance with best practices and works.