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

Brandon Wong
Brandon Wong
2,341 Points

Student records are not appearing

What did I do wrong?

Student records are not appearing when I search by name using this code.

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

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

function getStudentScore(student) { 
  var report = '<h2>Name: ' + student.Name + '</h2>'; 
  report += '<p>Track: ' + student.Track + '</p>'; 
  report += '<p>Achievements: ' + student.Achievements + '</p>'; 
  report += '<p>Points: ' + student.Points+ '</p>';
  return report;
} 


while (true) {
  search = prompt("Search for a student by name. To exit, type 'quit.'"); 
  if (search === null || search.toLowerCase() === 'quit') { 
  break;
  }
    for (var i = 0; i < students.length; i += 1) {
      student = students
      if (search === student.name) { 
      message = getStudentScore(student);  
      print(message);
      }
     } 
} 

2 Answers

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 51,979 Points

You have two issues:

1) In your for loop you assign student the entire array

student = students;

should be

student = students[i];

2) In your getStudentScore() function the student properties should be lowercase instead of title case. For example

student.Name

should be:

student.name