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

Brian Patterson
Brian Patterson
19,588 Points

Tried to add an else if statement if the student name is not in the list

Tried to add an else if statement if the student is not on the list. The problem I am having is that if the student is there it says they are not.

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

while (true) {
  search = prompt("Search for a student. Or type 'quit' to exit");
  if ( search === null || search.toLowerCase() === 'quit') {
    break;
  }
  for ( var i = 0; i < students.length; i += 1) {
    student = students[i];
    if ( student.name === search ) {
      message = getStudentReport( student );
      print(message);
    // } else if ( student.name !== search){
    //   message = '<h2>They are not in the class</h2>'
    //   print(message)
  } else if (search !== student.name ){
       message = 'The student named ' + search + ' is not at this class';
       print(message);
      }
    }
  }


print(message);

2 Answers

Steven Parker
Steven Parker
230,995 Points

You don't want to put out a "is not at this class" message for each time you compare the name with a student in the database, just one for any search that fails. So you need to wait until the loop is completely finished.

You could use a boolean value to indicate if anything was found in the loop. Then after the loop, you could test it to decide if the message should be shown.

Brian Patterson
Brian Patterson
19,588 Points

I have put the following code after the loop has finished.

if ( guess === false ) {
      message = '<h2>They are not in the class</h2>'
      print(message)
  }

It seems to work now. Thank for the tip Steven.

Cory Harkins
Cory Harkins
16,500 Points

Have you tried testing the search.toLowerCase() against the student.Name.toLowerCase()? You are using strict equality so there's no room for error. I could be mistaken but let me know what that does for you.

Brian Patterson
Brian Patterson
19,588 Points

I have copied what is in the JSON array and put it in the prompt dialogue box and it still fails. Might try what Steven has suggested and wait till the first if statement has finished.

Steven Parker
Steven Parker
230,995 Points

Just to be clear, I was suggesting waiting for the loop to finish. Then you know every student name has been checked.