Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Build an Object Challenge, Part 2 Solution

Brendan Moran
Brendan Moran
14,052 Points

Why not use students[i].property in the for loop?

I did my code almost identically to Dave's. Only thing is I didn't declare/define a student variable, I just used students[i].property throughout the loop. Is defining the variable DRY-er or more efficient in a larger program?

2 Answers

Karolin Rafalski
Karolin Rafalski
11,368 Points

I don't think it matters for the example given. However, if you had many students objects, i.e. [students2014, students 2015, students2016 etc...], it would be easier to create a new variable student, and then change the reference object, rather than changing all of your code from students to students2014 or students2015 each time you wanted to iterate over your desired data set.

James Falconi
James Falconi
5,375 Points

Thanks for bringing this up Brendan. I was wondering about this too and it was really bothering me. I could see that the creation of the variable wasn't wholly necessary in this particular instance. Obviously, more complicated scenarios would inspire you to look for other solutions. But, this exercise, being simple, could do without the variable. I really wish that this was addressed in the video lecture.