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 Multiple Items with Arrays Build a Quiz Challenge, Part 2

Franklin Colbert
Franklin Colbert
4,868 Points

My code doesn't loop through all my objects. Students.length, comes back undefined

var students = [
   {
   name: "Becky",
   track:"web Design",
   acheivments:33289,
   points:1322134
   },
    {
   name:"Bobby",
   track:"Javascript",
   acheivments:20000,
   points:12345
    },
    {
   name:"Billy",
   track:"Life Sciences",
   acheivments:69,
   points:69
    },  
    {
      name:"Byron",
   track:"Boombastic Beats",
   acheivments:777,
   points:99999
    },
    {
      name:"Frank",
  track:"Boss",
  acheivments:2,
  points:-2
    }
];


var message 

function print(message) {
 document.write(message)
};

for (i = 0; i < students.length; i +=1) {
      students = students[i];
      message += "<h2>Student: " + students.name + '</h2>';
      message += "<p>Track: " + students.track + "</p>";
      message += "<p>Acheivements: " + students.acheivments + "</p>";
      message += "<p>Points: " + students.points + "</p>";
      console.log(message);
};```

2 Answers

Franklin,

Your very close but its not students.length that is the issue its that you are using an undefined variable named students, and using the array variable name students at the same time. try changing it to this

  var message = ""; // empty string to put the iterating variable in. 
 var student;
function print(message) {
 document.write(message)
};

for (i = 0; i < students.length; i +=1) {
     student = students[i]; //change to a variable and change name
      message += "<h2>Student: " + student.name + '</h2>'; //change students to student
      message += "<p>Track: " + student.track + "</p>"; //change students to student
      message += "<p>Acheivements: " + student.acheivments + "</p>"; //change students to student
      message += "<p>Points: " + student.points + "</p>"; //change students to student
      console.log(message);
};

print(message);

Let me know if this helps. If not please feel free to follow up on your question.

akak
akak
29,445 Points

Hmm it seems that you need to have a new var to hold students[i] data. When you do students = students[i] you overwrite your students variable and that's why nothing comes out. Try using student = students[i] and don't forget to change it in your message as well.

  student = students[i];
      message += "<h2>Student: " + student.name + '</h2>';
      message += "<p>Track: " + student.track + "</p>";
      message += "<p>Acheivements: " + student.acheivments + "</p>";
      message += "<p>Points: " + student.points + "</p>";
      console.log(message);

I didn't check that, but hopefully it'll fix your problem :)