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

Alvaro López
Alvaro López
8,746 Points

This worked too...

var message = '';
var student;
var search = prompt("Search a student")

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

for (var i = 0; i < students.length; i += 1) {
  student = students[i];
  if (search === students[i].name) {
    message += '<h2>Student: ' + students[i].name + '</h2>';
    message += '<p>Track: ' + students[i].track + '</p>';
    message += '<p>Points: ' + students[i].points + '</p>';
    message += '<p>Achievements: ' + students[i].achievements + '</p>';
    break;
  } else if (search === 'quit') {
    break;
  } else {
    message = '<h1>You did not find students</h1>';
    var search = prompt("Search a student");
  }
}

print(message);

Hi Alvaro Lopez, I've put your code in a code block to make it easier to review.

1 Answer

This will only prompt the user up to the amount of times matching the length of the students array (i.e. the number of students). You should use a while loop to keep searching each time the user enters another name.

It also stops as soon as it finds someone with a matching name, but only if they get it exactly right (case sensitive). You should convert both the search query term and the name you're checking against to the same case so that it doesn't matter how they wrote it.

Also, you declare the student variable at the top, and assign it the value of the current student in the loop, but then you continue to just use the array and object properties instead. After you first set student = students[i] you can use student.name, etc to access that object.

Lastly, you should tell the user how they can quit. Not the most important thing when you're just testing it yourself, but it's a good habit to get into, having helpful messages to the user on how to use your program.