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

Now here is my code with matching username challenge complete, but I have one puzzling question.

I did it, but I have one question about the bottom of my code (see below):

// declare my variables up here so that they are defined once the window opens.
var message = '';
var student;
var search;
var results;

// function to print html to the window
function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
} 


/*function to print the student report. I didn't include an argument for this function.. Don't think I need to? */
function getStudentReport() {
  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 loop to run the loop so long as the condition is true. If the user cancels (null) or quits, the loop will break
*/
while (true) {
  search = prompt('Search student records:');
  if (search === null || search.toLowerCase() === 'quit') {
    break;
  } 
  var results = false;
  // for loop to cycle through the students array to match with the users search
  for (var i = 0; i < students.length; i++) {
    student = students[i];
    if (student.name.toLowerCase() === search.toLowerCase() ) {
      message += getStudentReport(student); //does there have to be an argument in here? Why?
      print(message);
      results = true;
    }
    if (!results) {
    message = "<h2> Sorry, that person does not exist in our records. </h2>";
    print(message);
    }
  }
}

I changed the last few lines of code to:

    if (!results) {
    print("<h2> Sorry, that name isn't in our system."</h2>);
    }
  }
message = "";
}

I did this so that I could reset the message, because in the first example, everytime I would search for "Jody" (the duplicate name), it would include my "Sorry, that name isn't in our system" along with both Jody's information. My question is why? The if event shouldn't have triggered, so why did the message variable pick up both event sentences?

I would really appreciate any help I can get on this.

1 Answer

The if statement actually does trigger, because it is checked each time through the loop; so although Jody exists, she doesn't in that location, and the condition is met. If you move that statement outside the for loop, you will make sure the whole array has been looped through before checking the condition.

Alex Flores
Alex Flores
7,864 Points

Thanks for the response, Suzanne. Forgive me for being a little slow on the uptake, but I don't really understand what you mean by the if statement is triggered. My understanding of how this code is read is as such.

  1. Search for Jody (she's in there twice).
  2. Go to first 'if' statement in the 'for' loop. condition is true, so the event is triggered. Turns 'results' to True.
  3. Check 2nd 'if' statement in 'for' loop. Condition is not true, event does not trigger bc the condition is not true.

Also, when I moved the last 'if' statement after the 'for' loop, the second Jody is not pulled.

  for (var i = 0; i < students.length; i++) {
    student = students[i];
    if (student.name.toLowerCase() === search.toLowerCase() ) {
      message += getStudentReport(student); //does there have to be an argument in here? Why?
      print(message);
      results = true;
    }
    }
      if (!results) {
      print("<h2> Sorry, that person does not exist in our records. </h2>");
  }
  message = "";
}

Thanks again for your help!