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 trialMohamed Ali
10,010 Pointsi 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);
wellingtonginah2
2,026 Pointswellingtonginah2
2,026 PointsHey, 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:
Cheers.