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

Yang Zou
Yang Zou
6,937 Points

Code only works when the last student's name is entered.. why?

The code below only works when I type 'Trish' which is the name of the last student. Anything else in the dialog box returns 'No result found'. Here's what's in my student_report JS file:

var message;
var student;
var search;

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

function getReport( 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 for a student by name or type 'quit' to exit");
  message = ' ';

//from here is where I got lost

  if( search === null || search.toLowerCase() === 'quit' ){
    break;
  } 

  for (var i = 0; i < students.length; i += 1) {
    student = students[i];

    if( search === student.name ) {
      message += getReport( student );
      print(message);
    } else {
    print("<h2>No result found</h2>");
  }
  }  
} 

I know two ways to fix this but don't understand why... The first one is (only copy-pasted the last part of the previous code):

  if( search === null || search.toLowerCase() === 'quit' ){
    break;
  } 

  for (var i = 0; i < students.length; i += 1) {
    student = students[i];

    if( search === student.name ) {
      message += getReport( student );
      print(message);
      break;
//add a break here and it works, but why?
    } else {
    print("<h2>No result found</h2>");
  }
  }  
} 

And the second is:

  if( search === null || search.toLowerCase() === 'quit' ){
    break;
  } else { 
    print("<h2>No result found</h2>");
  }
//move the 'else' bit here before the for loop and it works, again why??

  for (var i = 0; i < students.length; i += 1) {
    student = students[i];

    if( search === student.name ) {
      message += getReport( student );
      print(message);
    } 
  }  
} 

Please can anyone help me understand why my original code doesn't work and why the other two do? Many thanks in advance!!

1 Answer

akak
akak
29,445 Points

Hi Yang,

Your loop runs a few times depending of the number of student entries. Each time your if statement inside of it is evaluated. So for the first student it checks: is the name === search? No, then I go to else clause and print no student found. Is second student a match? No? Then I print no results found. In the case when the last student is the one you're looking for the previous "no results found" messages are overwritten by the student that was found. If the student is found earlier, it will get overwritten by "no results found" from evaluating the later students. So if you have 6 students your variable message will be changed 6 times for each student depending if he's found or not.

The first fix assumes that when the right student is found we end the loop. So another one won't be evaluated. As we know another one is not the one you are looking for (since you've found that one already) so the message variable, again, would be overwritten by no results found. As you brake from the loop that won't happen.

In the second fix you removed else clause so if the student is found the message variable will be populated, but each time it's not - the message won't be overwritten.

Yang Zou
Yang Zou
6,937 Points

Awesome! This has been bugging me for so long... Thank you so much akak for such a detailed explanation!