JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge Solution

3 Answers

James Estrada
James Estrada
Full Stack JavaScript Techdegree Student 21,459 Points

Nice solution!! You solved both problems the teacher left unsolved :)

I would add a suggestion to your code in one section of your while loop so it prints both found and not found students:

  noMatch = true;
  for (let i = 0; i < students.length; i++) {
    if (student === students[i].name.toLowerCase()) {
      noMatch = false;
      records += `<p><strong>Name: ${students[i].name}</strong></p> <br>`;
      records += `<p>Track: ${students[i].track}</p> <br>`;
      records += `<p>Achievements: ${students[i].achievements}</p> <br>`;
      records += `<p>Points: ${students[i].points}</p> <br>`;
      //print(records); // print them at the end of your while loop
      console.log(records);
    }
  }
  if (noMatch) {
    notFoundStudent = `The student ${student} is not found`; // you can add <p> tags to separate all not found students
    records += notFoundStudent; // adding this to your records variable will allow you to see a list of who was and wasn't found
    console.log(notFoundStudent);
    //print(notFoundStudent); // having both print statements in the case a student was found or not will cause an overwrite. Try searching for a match and no match with your original code to see what I mean
  }
  print(records); //this will print both found and not found students 
Steven Parker
Steven Parker
195,561 Points

Good job! And good use of the snapshot. :+1:

Two minor suggestions, when you need to use the same index of an array several times you can create a temporary variable to refer to that item directly. And paragraphs automatically add vertical space, so explicit <br> isn't needed:

      let s = students[i];  // simplify the remaining references
      records += `<p><strong>Name: ${s.name}</strong></p>`;
      records += `<p>Track: ${s.track}</p>`;
      records += `<p>Achievements: ${s.achievements}</p>`;
      records += `<p>Points: ${s.points}</p>`;
Alina Bylkova
Alina Bylkova
6,862 Points

Thanks for your recommendation! I will take it into account :)