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 trialLloyd Stevens
10,934 PointsMy solution - Objects "The Student Record Search Challenge"
Here is my solution to the student record search challenge from the end of the Objects tutorials for JavaScript.
If anyone could offer me some feedback it would be much appreciated.
Many thanks
Lloyd
Original Problem / Review
https://teamtreehouse.com/library/the-student-record-search-challenge-solution#questions
1 Answer
Steven Parker
231,248 PointsGlobalizing an index to allow access across functions violates common scope rules. It's far more conventional to declare loop variables directly in the loop and use them only there:
for (let i=0; i<students.length; i++) {
Following the "best practice" principle of separation of concerns, the code that checks if a record should be printed should be separate from a function that prints out a record. Then it would be possible to re-use the printing function from within a searching function.
And a function that acts on a student record could be passed the record as an argument, so an index is not needed:
function studentReport(student) { // output one student
for (const property in student) { // cycle through each property
Also, testing a specific property should be done outside of a loop that goes through each property.