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

Build an Object Challenge part 2, why does it print more than 5x object..

Hey guys,

this is what I did, but when i check the browser it prints multiple times and not just the 5 student objects. I dont know what's wrong..

var students =
    [
      {name: "Mark", track: "JavaScript", badges: 45, points: 1225},
      {name: "Jason", track: "Web Design", badges: 23, points: 525},
      {name: "Arjea", track: "IOS Development", badges: 43, points: 1232},
      {name: "Fahriel", track: "Frontend Development", badges: 32, points: 987},
      {name: "John", track: "Python", badges: 30, points: 817}
    ]

var studentRecord = " ";



  for (var i= 0; i < students.length; i += 1)
  {
    for (var studentKey in students)
      {
          studentRecord += "<ul><b> Student: " + students[i].name + "</ul></b>";
          studentRecord += "<ul>Track: " + students[i].track + "</ul>";
          studentRecord += "<ul>Points: " + students[i].points + "</ul>";
          studentRecord += "<ul>Badges: " + students[i].badges + "</ul>";
      }
  }
  document.write(studentRecord);

2 Answers

Hi Mark,

Try removing your for...in... statement inside your for..loop...

var students =
    [
      {name: "Mark", track: "JavaScript", badges: 45, points: 1225},
      {name: "Jason", track: "Web Design", badges: 23, points: 525},
      {name: "Arjea", track: "IOS Development", badges: 43, points: 1232},
      {name: "Fahriel", track: "Frontend Development", badges: 32, points: 987},
      {name: "John", track: "Python", badges: 30, points: 817}
    ]

var studentRecord = " ";

  for (var i= 0; i < students.length; i += 1)
  {
          studentRecord += "<ul><b> Student: " + students[i].name + "</ul></b>";
          studentRecord += "<ul>Track: " + students[i].track + "</ul>";
          studentRecord += "<ul>Points: " + students[i].points + "</ul>";
          studentRecord += "<ul>Badges: " + students[i].badges + "</ul>";
      }
  document.write(studentRecord);

Owa Aquino , thank you! I thought I still need to use that lol.

Do i only use that when I only have an object and I wanted to access the properties inside and not in an array of objects?