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

Student Search solution - Close but no cigar (need help)

I tried to check if 2 or more students have the same name. However, I end up printing the first instance of the name correctly. But the second instance of the name gets printed twice. Note: if only one instance of a name exists, it prints out nicely.

I'm not sure what I'm doing incorrectly. AFAIK, there's something funky with my forEach implementation (Line 31)

Would love help/tips on how you solved this.

var message = "";
var student;
var search;
var studentGroup = [];
var html = [];

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

function getStudentReport(studentGroup){
  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 'quit' to quit");

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

  for (var i = 0; i < students.length; i++){
    student = students[i];
    if (student.name.toLowerCase() === search.toLowerCase() ) {
      studentGroup.push(student);
      studentGroup.forEach(function(name) {
        html.push(getStudentReport(studentGroup));
        print(html);
      });
    }



    }



  //if you cannot find record in the array, print out a nice message 

  if (studentGroup.length === 0) {
    alert("We do not have that student in our system");
  }

}
//end while loop

2 Answers

Hi Ashi, why are you using a forEach loop inside your for loop? Your print function also should be modified to take a list of strings, can you see the comma that ends up between the outputted names?

html = []; // reset from previous search
for (var i = 0; i < students.length; i++){
    student = students[i];
    if (student.name.toLowerCase() === search.toLowerCase()) {
       studentGroup.push(student); // personally, I would remove this and just check html.length instead
       html.push(getStudentReport(student));
      // remove entire foreach
    }
}

print(html); // finished iterating, now print

Thanks Umesh. I was trying to use foreach to iterate through any duplicate results.

I think your way is more elegant. Trying it now :)

Since my original code was overly complex, rewrote thusly:

var message = "";
var student;
var search;
var studentGroup = [];  //check if name exists 
var html = "";

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>Points: " + student.points + "</p>";
  report += "<p>Achievements: " + student.achievements + "</p>";
  return report;
}

while (true) {
  search = prompt("Search student records: type a name [Jody] or 'quit' to quit");

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

  studentGroup = []; //reset after each search 

  //loop through the student records 
  for (var i=0; i < students.length; i++) {
    student = students[i];


    if (student.name.toLowerCase() === search.toLowerCase() ){
      studentGroup.push(student.name);
      html += getStudentReport(student);
      print(html);
      } 
    }


    //if you cannot find record in the array, print out a nice message 

     if (studentGroup.length === 0) {
       alert(studentGroup + "This student couldn't be found");
      }


}
//end while loop

:)

One step more if you'd like to try would be to just collect the students each iteration, then create a print method that takes a list of students and does all the html creation in there.