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 trialJordan Featherstone
5,973 PointsAnother solution? Nested loops, Good Idea?
I Have used a nested for in loop within the for loop to print out the data within the Un-ordered List.
var html = '<ul>';
for(var i =0; i<students.length; i+=1){
for (var prop in students[i]){
html += '<li>' + students[i][prop] + '</li>';
}
}
html += '</ul>';
document.write(html);
This to me seems a more particle solution because i'm not hard coding each property of the objects, am i correct to use this method or is this a bad practice?
Obviously i can make this more robust and 'Team friendly' by putting this into a function and another script. I was just wondering if this is a valid way to print of multiple objects?
Thanks J
1 Answer
Steven Parker
231,275 PointsIt depends on what functionality you want to achieve.
This method is good for showing the properties that exist for each student. But the original method would also show where expected properties might be missing on the student.
For example, if a student had no "track" property, your method would not show anything for track, but the original one would display "Track: undefined
".
So either method could be considered "valid", as long as difference in behavior fits the needs of the task.
Jordan Featherstone
5,973 PointsJordan Featherstone
5,973 PointsCould that not be easily solved with a if statement
I understand where you are coming from, Thank you for you reply.