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

belen almonacid
belen almonacid
8,495 Points

Please help. I cannot figure out how to print the students if the name is the same.

var message= '';
var student;
var search;
var inClass = false;


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

function studentReport (student) {
  var report = '<h2>Student: ' + students[i].name + '</h2>';
  report += '<p>Track: ' + students[i].track + '</p>'; 
  report += '<p>Achievments: ' + students[i].achievments + '</p>'; 
  report += '<p>Points: ' + students[i].points + '</p>'; 
  return report;
}

while (true) {
  search= prompt("Search for a student information by typing his/her name like 'Jody' 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.toLowerCase() === search.toLowerCase()) {
      inClass= true;
      message = studentReport( student );
      print(message);
    }
    if (!inClass){
    message = search + ' does not belong to the school.'
    print(message);
    }

  }
}

4 Answers

Umesh Ravji
Umesh Ravji
42,386 Points

Hey Belen, I think I misunderstood your question, and had thought that you were only having an issue displaying the results. The window.setTimeout is just there to make sure it displays in Chrome, you can read more about it here: http://stackoverflow.com/questions/19058858/jquery-append-in-loop-dom-does-not-update-until-the-end

There's room for improvement for sure, but one way to print multiple students (having the same name), is to store all of the matches into a foundStudents array, then use this array to generate the report via studentReports that I have modified to accept an array of students, rather than a single student. This was based on your original code.

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

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

function studentReports(students) {
  var report = '';
  for(var i = 0; i < students.length; i++) {
    report += '<h2>Student: ' + students[i].name + '</h2>';
    report += '<p>Track: ' + students[i].track + '</p>'; 
    report += '<p>Achievments: ' + students[i].achievements + '</p>'; 
    report += '<p>Points: ' + students[i].points + '</p>'; 
  }
  return report;
}

while (true) {
  search = prompt("Search for a student information by typing his/her name like 'Jody' or type 'quit' to exit.");
  if (search === null || search.toLowerCase() === 'quit') {
    break;
  }
  var foundStudents = [];
  for ( var i = 0; i < students.length ; i+= 1){
    student = students[i]; 
    if (student.name.toLowerCase() === search.toLowerCase()) {
      foundStudents.push(student);
    }
  }
  if (foundStudents.length === 0){
    message = search + ' does not belong to the school.'
    print(message);
  } else {
    message = studentReports(foundStudents);
    print(message);
  }
}
Umesh Ravji
Umesh Ravji
42,386 Points

Hi Belen, there's another thread about the current issues with this question in Chrome, you will find that this code runs in Firefox. Dave has written about it there: https://teamtreehouse.com/community/only-prints-after-break-statement-cant-figure-out-why

There's just one small issue you have to modify (below). You need to use the properties (name, track, etc) of the student object to create the report. The reason why it works in this case is because it reads the value of the i variable (global) and is able to generate the report using the current student using the students array. If i were to be changed, it would no longer work.

function studentReport (student) {
  var report = '<h2>Student: ' + students[i].name + '</h2>';
  report += '<p>Track: ' + students[i].track + '</p>'; 
  report += '<p>Achievments: ' + students[i].achievments + '</p>'; 
  report += '<p>Points: ' + students[i].points + '</p>'; 
  return report;
}

I've included my attempt at making this work in Chrome below, take a look at it if you wish to. I've taken out the while loop though.

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

function studentReport(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;
}

function performSearch() {
  var search = prompt('Search for a student..');
  if (search !== null && search.toLowerCase() !== 'quit') {
    var found = false;
    for (var i = 0; i < students.length; i++) {
      var student = students[i];
      if (student.name.toLowerCase() == search.toLowerCase()) {
        found = true;
        var message = studentReport(student);
        print(message);
        break;
      }
    }
    if (!found) {
      // do whatever if student cannot be found
    }
    window.setTimeout(performSearch, 0);
  }
}
performSearch();
belen almonacid
belen almonacid
8,495 Points

Thank you im going to try it out. But where do you come from with ' window.setTimeout(performSearch, 0); '?? And if i have 2 students with the same name maria.. this will print both name once the loop is done?

belen almonacid
belen almonacid
8,495 Points

thank you so much for taking your time!!! exellent!