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

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
216,810 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 = "";