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 trialXander Apponi
18,309 PointsA Flexible Solution (Please Review Code)
The solution provided by this video works great for the purpose shown, but what if another property is added? Say some students don't have a property for "Questions Asked" or some other possible value. Would it be better to create another loop that would go through all the possible property making it more flexible? I think I have created a solution...
var html = ""; //Creates the beginning of the html string
function print(message) { //A funtion that allows us to easily put this into the index.html document
document.write(message);
}
for (i = 0; i < students.length; i++) { //This loop accesses an entire object for each student
for (var prop in students[i]) { //This loop accesses each property of each student
if (prop === "Name") { //Makes the bolder than the other properties of each object
html += "<h2>Student: ";
html += students[i].Name;
html += "</h2>";
} else { //For all properties other than name
html += "<p>";
html += prop; //Example: track
html += ": ";
html += students[i][prop]; //Example: Ruby on Rails
html += "</p>";
}
}
}
print(html); //Calls function to write this to the page
Please let me know if my thinking is correct. Thanks.
1 Answer
Mike Nunyabeez
4,677 PointsWhere do you define students?
Xander Apponi
18,309 PointsXander Apponi
18,309 PointsIt is in another file