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

gary peart
gary peart
6,496 Points

Why is this code not working?

Can someone please tell me where I have gone wrong with the code below?

It is meant to find student records when one of the names in the object are searched for, but it never finds any of the names in one of the objects.

Thanks in advance for any direction you can give me.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Students</title>
</head>
<body>
<h1>Students</h1>
<div id="output">

</div>
<script src="students.js"></script>
</body>
</html>




function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}
var message = '';
var search;
var students = [
  {Name: 'John', Track: 'ios' , Achievements: 23, Points: 123 },
  {Name: 'Susan', Track: 'Web Design', Achievements: 12, Points: 333},
  {Name: 'Nazir', Track: 'Ruby', Achievements: 3, Points: 233},
  {Name: 'Rui', Track: 'Business', Achievements: 123, Points: 3435},
  {Name: 'Joao', Track: 'Perl', Achievements: 78, Points: 667}
];


function findStudent(student) {
  var report ='<h2>Student Found!</h2>';
  report += '<p>Name: ' + student.Name + '</p>';
  report += '<p>Track: ' + student.Track + '</p>';
  report += '<p>Achievements: ' + student.Achievements + '</p>';
  report += '<p>Points: ' + student.Points + '</p><br><br>';
  return report;
}

while (true){
  search = prompt('Please enter the name...');

  if (search.toLowerCase() === "quit"){
    break;
  }

  for(var i = 0; i < students.length; i += 1){
    var student = students[i];
    if(student.Name === search){
      message = findStudent(student);
      print(message);
    }
  }
}
Agustin Vargas
Agustin Vargas
10,896 Points

Looks fine and also worked when I ran a fiddle. Though you should add a toLowerCase() to student.name at the end there in your for loop.

jsdevtom
jsdevtom
16,963 Points

Agustin Vargas, could you post a link to the fiddle?

3 Answers

jsdevtom
jsdevtom
16,963 Points

Thank you :-) I just wanted to see this in action. I also can't see any problems with this. I think you hit the nail on the head with toLowerCase()

gary peart
gary peart
6,496 Points

Hi Agustin,

I realised where I went wrong was as you said, not adding the toLowerCase() to student.Name. By not doing that my code failed every time because I entered the whole name into the prompt box in lower case.

A big thank you for replying though, greatly appreciated!!!!

Gary.

Agustin Vargas
Agustin Vargas
10,896 Points

No problem at all, it's always the little details that'll getcha.