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

Nour El-din El-helw
Nour El-din El-helw
8,241 Points

'Undefined'

I always get 'undefined' printed whenever i run the program. Here is my code :

var students = [
  {
    name : 'Nour El-din',
    track : 'Front-end web development',
    achievements : 5,
    points : 3000
  },
  {
    name : 'Mariam',
    track : 'IOS',
    achievements : 6,
    points : 1000
  },
  {
    name : 'Hala',
    track : 'Android',
    achievements : 8,
    points : 300
  },
  {
    name : 'Heba',
    track : 'Web Design',
    achievements : 10,
    points : 30000
  },
  {
    name : 'Ahmed',
    track : 'C#',
    achievements : 15,
    points : 5000
  }
];

var messege;

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

for (var i = 0; i < students.length; i++) {
  for (var key in students[i]) {
    if ( students[i][key] === students[i].name ) {
      messege += '<h2>' + key + ' : ';
      messege += students[i][key] + '</h2><br>';
    } else {
        messege += key + ' : ';
        messege += students[i][key] + '<br><br>'
      }
    }
  }

print(messege);
Nour El-din El-helw
Nour El-din El-helw
8,241 Points

Sorry if I didn't provide a snapshot of the workspace as I'm working in my editor.

Hey check your variable name. I think you misspelled "message". your code is saying messege in two places

1) var messege 2) print(messege);

other than that I think you're golden.

1 Answer

Steven Parker
Steven Parker
231,072 Points

The spelling of "messege" is not an issue because it is used consistently in the program.

The reason "undefined" appears in the output is because the variable is appended ("+=") before it is assigned. This can be fixed by just initializing it as a blank string:

var messege = "";