JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Build an Object Challenge, Part 2 Solution

Marcin Lipski
Marcin Lipski
8,625 Points

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
203,729 Points

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

teofanescucosmin
teofanescucosmin
13,643 Points

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 += .