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
jimvichos
4,700 Pointsit doesn't work i cant figure out whats the problem it shows only one object not 5
it doesn't work i cant figure out whats the problem it shows only one object not 5
var message = ''; var students;
function print(message) { var outputDiv = document.getElementById('output'); outputDiv.innerHTML = message; }
for(var i = 0; i < student.length; i += 1) { students = student[i]; message = "<h1>" + "name: " + students.name + "</h1>"; message += "<p>" + "Tracks: " + students.track + "</p>"; message += "<p>" + "Achievements: " + students.Achievements + "</p>"; message += "<p>" + "Points: " + students.Points + "</p>"; }
print(message);
2 Answers
andren
28,558 PointsThe issue is this line:
message = "<h1>" + "name: " + students.name + "</h1>";
By using = you replace the existing content of the message variable with the one you are assigning to it. Which means that the content that was in message during the previous loop is lost. The line is meant to use += like this:
message += "<h1>" + "name: " + students.name + "</h1>";
That way the content is just added to message without replacing what was already present in the variable.
izzy goldman
12,542 PointsIs student a object of objects Or an array of Objects?