Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

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
210,424 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"?