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

Erik Nuber
Erik Nuber
20,629 Points

Improving the code shown in the video with slight modification

Not a question, just a suggestion by adding in a single snippet of code to make this work a titch better.

Following along with the Video, I couldn't figure out why my code wasn't working when it was basically identical. But, once going along with the video, I realized it is because when prompted, I wasn't matching the student's name exactly as it was written. As such, the code really needs another toLowerCase() or, a single line of code that permanently changes the variable "search" toLowerCase() as ultimately the variable result does not effect putting out the Students Info which is stored correctly.

var message = "";
var student;
var search;


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

function getStudentReport(student) {
  var report = "<h2>Student: " + student.name + "</h2>";
  report += "<p>Track: " + student.track + "</p>";
  report += "<p>Achievments: " + student.achievements + "</p>";
  report += "<p>Points: " + student.points + "</p>";
  return report;
}

while (true) {
  search = prompt('Search student records: type a name (or type "quit" to end)');
  search = search.toLowerCase();
  if (search === "quit" || search === null) {
    break;
  }  
  for (var i = 0; i < students.length; i++) {
    student = students[i]; 
    if (student.name.toLowerCase() === search) {
        message = getStudentReport(student);
        print(message);
        }
  }
} 

print(message);