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

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.