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'm using the solution file in the project files and noticed that if you don't capitalize the names, nothing will appear

on the web page. In other words, on prompt I type "jody" and then "quit" and nothing displays but if I type "Jody" and then quit, all of Jody's info displays. I don' understand why because the file contains the .toLowerCase() method.

Could you post the code here?

It seems that search.toLowerCase() only transforms search to lowercase to compare it to quit. When you run the code, it then checks student.name in the if statement and compares it what you typed which has to be exact. You have to call toLowerCase() on both the search prompt and the student name being pulled from the object array.

Below is a working example. I hope this helps :)

var message = '';
var student;
var search;

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>Points: ' + student.points + '</p>';
  report += '<p>Achievements: ' + student.achievements + '</p>';
  return report;

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

1 Answer

Yep, I ran on this "problem" to. I couldn't see what I had to do until I I saw Christopher's solution. But I added one simple OR condition that let you type in all lower case:

 for (var i = 0; i < students.length; i += 1) {

  student = students[i];
  if ( student.name === search || student.name.toLowerCase() === search ) { /*Here We check if search its equal 
to one of our users stored in the object, then the OR condition (as Christopher said) convert the 
NAME ON THE OBJECT to lowercase and compare it with the prompt. Not the other way around.*/
    message = getStudentReport(student);
    print(message);


  }

Thanks a lot Christopher!