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 trialKeith Walker
5,076 PointsIs it common practice to name things so closely?
Student being used in both the function parameter, then as a variable, and students being named as the array seems to be needlessly confusing. Wouldn't it be easier to read the code if things were more easily distinguishable from each other? Now I'm not sure if student being used as the getStudentReport parameter is intentional, due to student being redefined as students[i] in the for loop. I've been being taught that things like organizing, naming, and making your code easily readable is very important, but I'm not sure how this helps.
1 Answer
Eric Butler
33,512 PointsGood question, Keith, and yes, it is pretty common. I can see where writing "student" over and over again could seem overly redundant/confusing, but in that video there's actually only 2 "entities" if you will (I don't know what else to call it):
- students (the whole group) and
- student (a dynamic representation of one of those students).
The var student;
at the top is actually the same student
variable that's in student = students[i]
and student.name
, student.track
, etc. The reason you have to write students[i]
is because you are picking one of the students out of the group to talk about at a time. In the physical world, it might go like this (and I think you'll agree it makes sense to say "student" over and over again this way, too):
Say you're looking at a group of students in a classroom. Collectively you call them "students," because that's what they are. You point to the first one, and say, "This student, her name is Kelly. She has 100 points." Then you point at someone else and say, "This student, his name is Kyle. He has 40 points." And if you had to figure out how many times you have to repeat ("loop") pointing at each student, you'd have to count how many total students there are: that's done in the code by writing students.length
.
Does that help at all? You could change the names of these variables so the group isn't just a plural of the singular, but that might actually be more confusing, since that's exactly what these 2 things represent.