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 Data Using Objects The Build an Object Challenge, Part 1

Ned Redmond
Ned Redmond
5,615 Points

What is my code missing? It will only print the final key/value in the final object in my array!

Here's what I have. All that will print is "points: 1990" and I don't understand why. :/

var students = [
  {
  name: 'Ned',
  track: 'JavaScript',
  achievements: 99,
  points: 9999
  },
  {
  name: 'Pete',
  track: 'CSS',
  achievements: 6,
  points: 666
  },
  {
  name: 'Fiesta',
  track: 'Party',
  achievements: 69,
  points: 6969
  },
  {
  name: 'Frida the Cat',
  track: 'Cat Stuff',
  achievements: 4,
  points: 999
  },
  {
  name: 'Nas',
  track: 'Wife is Nice',
  achievements: 302,
  points: 1990
  }
]

for ( i = 0; i < students.length; i++) {
  for ( key in students[i] ) {
    document.getElementById('output').innerHTML = '<p>' + key + ': ' + students[i][key] + '</p>';
  }
}

2 Answers

Ned Redmond
Ned Redmond
5,615 Points

Answering my own question. It should have been "+=" and not "=" within the for/in loop.