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 2

Mohamed Ali
Mohamed Ali
10,010 Points

i did it in a nother way!

var students = [ { name: 'Dave', track: 'Front End Development', achievements: 158, points: 14730 }, { name: 'Jody', track: 'iOS Development with Swift', achievements: '175', points: '16375' }, { name: 'Jordan', track: 'PHP Development', achievements: '55', points: '2025' }, { name: 'John', track: 'Learn WordPress', achievements: '40', points: '1950' }, { name: 'Trish', track: 'Rails Development', achievements: '5', points: '350' } ];

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

for (var key in students) { var message = '<h2>Student: ' + students[0].name + '</h2>'; message += '<p>Track: ' + students[0].track + '</p>'; message += '<p>Achievements: ' + students[0].achievements + '</p>'; message += '<p>Points: ' + students[0].points + '</p>';

message += '<h2>Student: ' + students[1].name + '</h2>'; message += '<p>Track: ' + students[1].track + '</p>'; message += '<p>Achievements: ' + students[0].achievements + '</p>'; message += '<p>Points: ' + students[1].points + '</p>';

message += '<h2>Student: ' + students[2].name + '</h2>'; message += '<p>Track: ' + students[2].track + '</p>'; message += '<p>Achievements: ' + students[0].achievements + '</p>'; message += '<p>Points: ' + students[2].points + '</p>';

message += '<h2>Student: ' + students[3].name + '</h2>'; message += '<p>Track: ' + students[3].track + '</p>'; message += '<p>Achievements: ' + students[0].achievements + '</p>'; message += '<p>Points: ' + students[3].points + '</p>';

message += '<h2>Student: ' + students[4].name + '</h2>'; message += '<p>Track: ' + students[4].track + '</p>'; message += '<p>Achievements: ' + students[0].achievements + '</p>'; message += '<p>Points: ' + students[4].points + '</p>'; }

print(message);

Hey, Mohamed!

While your code certainly works, it isn't really following DRY (don't repeat yourself) principles. Dry code is important because it allows you to more easily return to code to make changes.

If you find yourself repeating something over and over again with small changes, it is a prime candidate for a loop. You can use a for loop to access all of the student objects in your students array. Then, you can access each property of an individual student object using a for in loop.

I'm learning too, so while this isn't perfect, something like this is a bit better as it leverages the advantages of loops, arrays, and objects:

var html = ''

function studentReport() {
  for (var i = 0; i < students.length; i++) {
    for (var prop in students[i]) {
      if (prop === 'name') { 
        html += '<b> Student: ' + students[i][prop] + '</b>'
      } else {
        html += '<p>' + prop +': ' + students[i][prop] + '</p>'
      }
    }
    html += '<br>'
  }
}

studentReport();
document.write(html);

Cheers.