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 2

Matthew Goldwater
Matthew Goldwater
3,859 Points

Why does my output start with undefined?

Everything else in my code works well. I just can't figure out why it says undefined at the top of my page when I preview it. I'd appreciate any help.

Here's my code:

var students = [
  {name: 'Tom', track: 'ios' , achievements: 4, points: 23},
  {name: 'Bob', track: 'ux', achievements: 5, points: 34},
  {name: 'Zoe', track: 'ui', achievements: 2, points: 76},
  {name: 'So',  track: 'web', achievements: 1, points: 52},
  {name: 'Hal', track: 'osx', achievements: 9, points: 98}
];

var html;

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

function buildContent(arr) {
  for (key in students) {
    html += '<p>' + '<strong>' + 'Student: ' +  
students[key].name + '</strong>' 
+ '<br><br>' + ' Track: ' 
+ students[key].track + '<br><br>' 
+ ' Points: ' + students[key].points 
+ '<br><br>' +  ' Achievements: ' 
+ students[key].achievements 
+ '<br><br>' + '</p>'
  }
  return html;
}
print(buildContent(students)); 

1 Answer

You should declare 'var html' as an empty string: var html = ' '.

I am not sure why is that, but had the same problem yestarday ;)