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!

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

Why is my code not working when I use print(), but it works with document.write?

var students = [ {name:'A', track:'java', achievements: 2 , points: 10 }, {name:'B', track:'js', achievements: 3 , points:100 }, {name:'C', track:'ruby', achievements: 5 , points: 1000}, {name:'D', track:'css', achievements: 6, points: 10000 }, {name:'E', track:'html', achievements: 7 , points: 100000}


function studentRecords(records) { var listHTML =' '; for ( var prop in students ) { listHTML+='<h2><em>Name: '+ students[prop].name + '</em></h2>'; listHTML+='<p>Track: '+ students[prop].track + '</p>'; listHTML+='<p>Achievements: '+ students[prop].achievements + '</p>'; listHTML+='<p>Points: '+ students[prop].points + '</p>'; }

print(listHTML); } studentRecords(students);

2 Answers

Steven Parker
Steven Parker
227,126 Points

It looks like the definition for the "print" function is missing.

function print (message){ document.getElementById('output').innerHTML += message; };

If your print function was like the one i use here and on your html was printed only last message is becouse after .innerHTML you put a simple = not += .