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

My Solution but unsure about the method

Here is my solution to part 2 of the challenge. My method was to simply print the properties from a simple "for" loop. I am not sure if this is the right way to do it and if the method makes it inflexible.

Personally i am struggling with combining loops with Arrays that contain objects).

I would appreciate any feedback on it.

Thanks

Harry

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' }
];

document.write("<h2>" + "Student List" + "</h2>");

for (var i = 0; i < students.length; i += 1){
  document.write('<h4>' +[i] + ". " + students[i].name + '</h4>' + "Track " + ": " + students[i].track + "<br>" 
  + "Achievements " + ": " + students[i].achievements + "<br>"
  + "Points " + ": " + students[i].points
  );
}//noprotect

2 Answers

Hi, looks good to me, I would personally output it a little differently for example :

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' }
];

document.write("<h2>" + "Student List" + "</h2>");

// Difference below
for (var i = 0; i < students.length; i += 1){
// Create a variable for output and clear the string for each loop
  var studentString = "";
  studentString += '<h4>' +[i] + ". " + students[i].name + '</h4>';
  studentString += "Track " + ": " + students[i].track + "<br>" 
  studentString += "Achievements " + ": " + students[i].achievements + "<br>"
  studentString += "Points " + ": " + students[i].points
  document.write(studentString);
}//noprotect

Just looks cleaner, hope it helps.

Dale that looks so much better. I forgot about updating a variable ( += ) with new info.

Thanks

No worries :)