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

How can I stop this from printing 'Student Not Found' for each iteration of [i] where student.name != search?

I think I understand why the if...else statements in the for loop are causing the problem, but nothing I've tried seems to fix it. Does anyone have any suggestions?

I really just want it to print "Student Not Found" once if the name isn't in the students.js file.

link to source

var message = '';
var student;
var search;

function print(message) {
 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: type a name [Jody], or type 'quit' to end.");
  document.getElementById('output').innerHTML = "";
  if (search === null || search.toLowerCase() === 'quit') { break;}
  for (var i=0; i<students.length; i++) {
    student = students[i];
    if (student.name.toLowerCase() === search.toLowerCase()) {
      message = getStudentReport(student);
      print(message);
    } else {
        print('Student Not Found');
    }
  }
}
Steven Parker
Steven Parker
229,786 Points

The "link to source" was a good idea, but...

:x: You can't share a direct URL to your workspace, it's temporary and only exists while you are using it.

:information_source: But you can use the snapshot function in the workspace and provide the link to that.

Thanks Steven Parker, I made that change - it should work correctly now.

1 Answer

Steven Parker
Steven Parker
229,786 Points

:point_right: You need to print your message after the loop, not inside it.

You can set a boolean in the loop if you find anything, and test it to see if the message should be used at all:

  var notfound = true;    // assume none will be found
  for (var i=0; i<students.length; i++) {
    student = students[i];
    if (student.name.toLowerCase() === search.toLowerCase()) {
      message = getStudentReport(student);
      print(message);
      notfound = false;   // remember we found at least one
    }
  }
  if (notfound) {
    print('Student Not Found');
  }

Aha! Thanks again!