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 trialAidi Xu
5,737 PointsUncaught 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
231,236 PointsThe 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"?