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

Solution to extra problem

var response = "";
var searchQuery = "";
var studentFound = false;

while ( true ) { 
  searchQuery = prompt("Enter a student name. Type 'quit' to exit");

  if (searchQuery === null || searchQuery === 'quit') {
    break;
  }
  else {
    // re-initiate response message 
    // and studenFound for every search query
    response = "";
    studentFound = false;

    // search through all students to find a match 
    for (var i=0; i<students.length; i++){

      // if there is a match
      if( students[i].name.toLowerCase() === searchQuery.toLowerCase()){

        // change flag to true
        studentFound = true;

        // concatenate student report
        // to print all student with the same name
        response += getStudentReport(i);
      }
    }

    // if there is no match
    if( ! studentFound ) {
      response = notFoundMessage(searchQuery);
    }

    // print response
    print(response);
  }
}

function getStudentReport(studentId) {
  // get student report
  var report = "<h2>Students: " + students[studentId].name + "</h2>";
  report += "<p>Tracks: " + students[studentId].tracks.join(", ") + "</p>";
  report += "<p>Points: " + students[studentId].points + "</p>";

  return report;
}

function notFoundMessage(searchQuery){
   return "Sorry.. We don't have a student named " + searchQuery + ".";
}

function print(message) {
  var divHTML = document.getElementById("output");
  divHTML.innerHTML = message;
}

1 Answer

David Kanwisher
David Kanwisher
9,751 Points

It was nice to see someone who came up with a similar solution.

Correct me if I'm misunderstanding:

I think you need something in your code that resets your studentFound = false for each new search

If I Search for Xavier( Not on our list), the studentFound remains false and correctly gives us the "sorry..." message

if I search Jody (On our list), the studentFound changes to " true", and also correctly returns Jody's info

If I search Hank Hill (Not on our list), the studentFound remains true (from previous the last search), and will display a blank message rather than the intended "sorry..." message

aahh.. good catch.. I've edited the code to also re-initiate studentFound for every search query.. thanks David! (: