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 Solution

Tobi Ogunnaike
Tobi Ogunnaike
2,242 Points

Why does Dave need to create a 'student' variable in this challenge?

I wrote this , and it works fine.

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

Is there a specific problem Dave tried to avoid by creating and using a variable called student as below?

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

Thanks!

1 Answer

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

I think it's just about creating DRY code. Code that doesn't repeat. In Dave's code he uses iteration but only once. In your example you're using iteration for each property in your object.