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

Aidi Xu
Aidi Xu
5,737 Points

Uncaught TypeError: Cannot read property 'name' of undefined, Why???

const records = [
  {name: 'John', track: 'Web Design', achievements: 90, point: 89 },
  {name: 'Andy', track: 'iOS', achievements: 98, point: 99 },
  {name: 'Sandy', track: 'Front End Development', achievements: 88, point: 95 },
  {name: 'Amenda', track: 'Interaction Designer', achievements: 95, point: 95 },
];

let message;
let student;
let search;

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

while(true){
 search = prompt('Please enter the student name you want to see his/her record. Enter 
 quite if you want to quite the search.')
 if(search.toLowerCase() === 'quite'){
  break;
  }
 for(let i = 0; i <= records.length; i += 1){
  student = records[i];
  if(student.name === search){
      message += '<h3>student:' + student.name + '</h3>';
      message += '<p>track:' + student.track + '</p>';
      message += '<p>achievement:' + student.achievements + '</p>';
      message += '<p>point:' + student.point + '</p>'; 
  }
    }    
  }

 print(message);

1 Answer

Steven Parker
Steven Parker
202,065 Points

The search loop runs too many times, one more than you have records:

 for (let i = 0; i < records.length; i += 1) {    // use < instead of <=

Also, did you mean to write "quit" instead of "quite"?