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 Student Record Search Challenge Solution

Search function not working, also don't fully understand the teacher's solution

So I completed this challenge first with a very inelegant solution that only used one loop to make the prompts run and then printed the results manually. This worked, but as I say, it was not very clever or elegant.

After watching the teacher's solution I tried to copy it to try and understand it, but now my code doesn't run and I can't figure out what is wrong with it, as I am not getting any errors. The prompts appear, but if I type in a student name and type "quit", the student records are no longer displayed.

My code snapshot: https://w.trhou.se/bp73m3li78

(Please ignore all the commented out code at the bottom, this was just experimentation).

I also don't really understand how the "for" loop works within the "while" loop to search the array for the results. I understand that the "while" loop makes the program run continuously until quit is typed. But as I understand the "for" loop, it iterates between the different objects in the array each time the prompt runs. So is there not a chance that if I type in the 5th name in the array (in this case, "Jimbo"), the "counter" (or iteration) of the loop will currently be on the first name (or the second, third or fourth name for that matter), and therefore not find the result?

So to summarise, I'd be very grateful if someone could please explain:

1) why my code doesn't print the student results

2) how the "for" loop within the "while" loop works (and how loops within loops work more generally)

Thanks

1 Answer

Hi Sam, You had an error on line 33. The error does not appear in the browser for some reason, but the error was: this:

    for (var i = 0; i < students.length; i += 1) {
       let student = students[i];
     if ( student.Name === search ) { // On this line you had student.name with a lowercase n. You should've had an uppercase N.
       message = getStudentReport( student );
       print(message);
    };
  };
  };
  1. The for loop loops over all of the students in the students array, and if the student name matches the search it prints it to the screen. Hope this helps

Best Regards, Glaukio