Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,017 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 += .