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,228 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,228 Points

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

Brian Johnson
Brian Johnson
Front End Web Development Techdegree Graduate 18,997 Points

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
177,602 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 = "";