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

Rebecca Winterfield
Rebecca Winterfield
Full Stack JavaScript Techdegree Student 5,993 Points

Only the first student is printing.

The first student is printing, but the rest are not. Can someone please help me find out why? Thank you!

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

var student = [
  {
    name: "Peter",
    track: "Full Stack",
    achievements: 94,
    points: 399
  },
  {
    name: "Rebecca",
    track: "Front End",
    achievements: 300,
    points: 2003
  },
  {
    name: "Alejandro",
    track: "Web Design",
    achievements: 40,
    points: 294
  },
  {
    name: "Nina",
    track: "Back End",
    achievements: 469,
    points: 39960
  },
  {
    name: "Nick",
    track: "JavaScript Dev",
    achievements: 300,
    points: 20940
  }    
];


for (var i = 0; i < student.length; i += 1 ) {
  message = '<h2>Student: ' + student[i].name + '</h2>';
  message += '<p>Track: ' + student[i].track + '</p>';
  message += '<p>Achievements: ' + student[i].achievements+ '</p>';
  message += 'Points: ' + student[i].points+ '</p>';
};

print(message);

1 Answer

Steven Parker
Steven Parker
177,602 Points

I think you meant to say that only the last student ("Nick") is printing. And that's because the first line in your loop is a simple assignment ("=") that replaces anything already in "message" with new contents.

If you change that into an addition assignment ("+=", as used in the other lines) then the message will contain all student records.

Gustavo Winter
Gustavo Winter
Pro Student 27,382 Points

Hello Steven Parker, depends on what she wants the variable Message holds, if you add the ("+=") message will holds all the student that have already passed through the loop and if you console.log(message) It'll show all the student at once.

But if Rebecca Winterfield wants to show every student indivudually, separated from the rest she need to keep the ("=").

By the way, you only print one student with the variable message, because it's outside the loop, you need to bring it inside the loop, so that every time the loop occurs the print(message) will execute with the student[i].

for (var i = 0; i < student.length; i += 1 ) {
  message = '<h2>Student: ' + student[i].name + '</h2>'; // Or "+=" if you want print all the student at once, like one single object.
  message += '<p>Track: ' + student[i].track + '</p>';
  message += '<p>Achievements: ' + student[i].achievements+ '</p>';
  message += 'Points: ' + student[i].points+ '</p>';
  print(message);
};
Steven Parker
Steven Parker
177,602 Points

Actually, moving the "print" into the loop will not make a difference unless you also change the way it works. It also uses a simple assignment to replace the contents of the output div.

But if you change that assignment into an addition assignment, then your suggestion would produce the same results as mine.