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

I have done the extra challenge but I have a bug, I can't see why I have to type twice 'quit' to leave the program.

var message = [];
var student;
var searchBox;


function print(message){
      var outputDiv= document.getElementById('output');
      outputDiv.innerHTML =message;
}

 function getStudentReport(student){
     var report= '<h2> Student: ' + student.name + '</h2>';
      report += '<p> Track: ' + student.track + '</p>';
      report += '<p> Achievement: ' + student.achievement + '</p>';
      report += '<p> Points: ' + student.points + '</p>';  
     return report;
 }
  while(true){
          searchBox = prompt('Search for student or type quit to leave the prompt'); 
          if( searchBox.toLowerCase() === 'quit'){
          break
          }   

    for(var i =0; i< students.length; i++){
              student = students[i];
          if(student.name === searchBox){
           message.push(getStudentReport(student));
         }
          if (student.name !== searchBox){
         searchBox = prompt( searchBox +" is not a student, try again. Search for student or type quit to leave the prompt");
           break
         }else{
         print(message);
         }    
      }    
  } 

1 Answer

Steven Parker
Steven Parker
229,744 Points

There are two different prompts in this code, and only one of them checks for "quit".

The response to the prompt that contains "is not a student" is ignored, and the other prompt will always be issued following it.

You might want to replace the second one with an "alert".

That might solve the immediate question, but you'll probably want to take a closer look at the overall logic. There are a number of other issues that will impede proper search operations.

Uche Onuekwusi
Uche Onuekwusi
17,817 Points

Please why did you have to break at the last if statement. I thought you can only break out of loops not conditional statements

Steven Parker
Steven Parker
229,744 Points

You're right, the "break" ends the loop — the one that surrounds the conditional statement.