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

Hunter G
Hunter G
6,612 Points

Can't get non-existing student code to work. .

https://w.trhou.se/95tnh7rkvt

For some reason, you have to type your answer twice for this to properly work. I've looked around at other forum posts for answers but people either don't have the correct answer or use a different kind of loop. I added the else statement in student_report.js from lines 27-31. . Can somebody help me fix this? :)

1 Answer

Steven Parker
Steven Parker
231,007 Points

:point_right: I noticed two possible causes of double input.

One is on line 28 where it says "if (search !== student)", but student has not been assigned yet. Another is that the "not found" message is given as a prompt instead of a normal alert. That use of the timeout seemed like a potential cause of trouble also.

Here's how I might revise promptSearch:

function promptSearch() {
  while (true) {
    search = prompt("Search for a student's records in our database. Enter the student's name to see if his or her records exist. Otherwise, type 'quit' to exit.");
    if (search === null || search.toLowerCase() === 'quit') {
      break;
    }
    var unfound = true;
    for (var i = 0; i < students.length; i += 1) {
      student = students[i];
      if (student.name === search) {
        htmlOutput = getStudentReport(student);
        print(htmlOutput);
        unfound = false;
      }
    }
    if (unfound) {
      search = alert("Sorry, but that student does not exist in our records.");
    }
  }
}
Hunter G
Hunter G
6,612 Points

instead of declaring another variable, is it possible to get this to work with what I have already? Is there anyway I can use

if (search !== student) {

}

if the student wasn't found?

Also, using the setTimeout works great, it allows the records to show up for me in real time. Before the student records wouldn't show for me until I typed in 'quit'. I saw somebody recommend this in another forum post and to put the function execution on line 19 above the loop instead of inside of it