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

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

Samantha Atkinson
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Samantha Atkinson
Front End Web Development Techdegree Graduate 40,307 Points

I don't understand why when I print to my doc the first student has: underfinedStudent 1.

var student = [

{ studentNo: 1, name: "Janet Ray", track: "iOS", achievements: 50, points: 10345 },

{ studentNo: 2, name: "Rudy Modeste", track: "Wordpress", achievements: 35, points: 7678 },

{ studentNo: 3, name: "Louise Right", track: "Front End Web Development", achievements: 75, points: 15123 },
{ studentNo: 4,
name: "Cathy Kendo", track: "Web Design", achievements: 40, points: 9987 },

{ studentNo: 5, name: "Matt Atkinson", track: "Java Web Development", achievements: 80, points: 20764 }

];

var stuPerson; var html;

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

           for ( var i= 0; i < student.length; i+= 1){
             stuPerson  ="Student: " + student[i].studentNo + "<br>";
             stuPerson  +="Name: " + student[i].name + "<br>";
             stuPerson +="Track: " + student[i].track + "<br>";
             stuPerson +="Achievements: " + student[i].achievements + "<br>";
             stuPerson += "Points: " + student[i].points +"<br><br>";
             html += stuPerson;


            }

//5 Print out all student info

print(html); '''

2 Answers

Austin Britton
Austin Britton
6,531 Points

It has to do with the fact that when your declared 'var html' you didn't define it and where its being addressed from inside the function. I think your tacking the student information onto the end of the 'undefined' value inside 'html' from when you declared it. Two ways to solve off the top of my head

If you set it equal to an empty string when you declare it, you'll find yourself without an undefined student!

or you can make your loop a function, create a local object, return that to a variable and pass it off to your print function where message will no longer be undefined

Hope this helps!