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

Alex Flores
Alex Flores
7,864 Points

Not sure why this isn't working. Help is appreciated

I'm trying to get the alternative message to show when the user searches for a user that isn't in the record list, but I can't seem to get it.

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:');
  if (search === null || search.toLowerCase() === 'quit') {
    break;
  } 
  for (var i = 0; i < students.length; i += 1) {
    student = students[i];
    if (student.name.toLowerCase() === search.toLowerCase() ) {
      message = getStudentReport(student);
      print(message);
    }
    if (student.length == 0) {
      message = "<h2> Sorry, they don't exist </h2>";
      }
    }
  } 

2 Answers

Erik Nuber
Erik Nuber
20,629 Points

Haven't gotten to arrays yet so I am only offering suggestions based on less knowledge and from what I see...maybe something will help.

You use "student" and "students" as variables. Students is not defined which is what I believe Colton was pointing out. You responded saying that it is an array held in another .js file but, there is nothing that I see tying the other file to this.

with this in mind, the " i < students.length " statement isn't defined and, is getting an error message as just that.

It also looks like the last if statement should be an else if statement, maybe thats why it isn't going to that part?

Where is students coming from?

Alex Flores
Alex Flores
7,864 Points

Sorry, I was rushed for time when I posted this - had to catch the train. Students is the name of the javascript file (students.js) which holds the student record information inside the array students. I used the same file and most of the code from the video example.