JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge Solution

Sam Bailey
Sam Bailey
5,002 Points

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:

(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)


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 with a lowercase n. You should've had an uppercase N.
       message = getStudentReport( student );
  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