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 trialkevinkrato
5,452 PointsSearch function is not retrieving information
The code does not produce any console errors. However, it is also not finding the student.name that I enter. Where is my code going wrong? I've spent a good bit of time trying to find the error but im getting fatigued. :(
var studentData = [
{ name:'Kevin', track:'JavaScript', achievements:18, points:2230 },
{ name:'ScaryTerry', track:'Java', achievements:22, points:2731 },
{ name:'Rick', track:'HTML', achievements:131, points:17311 },
{ name:'Morty', track:'HTML', achievements:13, points:1341 },
{ name:'Jerry', track:'CSS', achievements:0, points:13 }
];
function print(message) {
document.write(message);
}
function getStudentReport( student ) {
var report = '<h2> Student: ' + student.name + '</h2>';
report += '<p> Track: ' + student.track + '</p>';
report += '<p> Achievements: ' + student.achievements + '</p>';
report += '<p> Score: ' + student.points + '</p>';
return report;
}
var student;
var message = '';
var search;
while(true) {
search = prompt('Search student records: type a name [Jerry] (or type "quit" to exit)');
if (search === null || search.toLowerCase() === 'quit') {
break;
}
for(var i = 0; i < studentData.length; i += 1) {
student = studentData[i];
if( studentData.name === search ) {
message = getStudentReport( student );
print(message);
}
}
}
1 Answer
Steven Parker
231,248 PointsIn the search loop:
if( studentData.name === search ) {
The array "studentData" doesn't have a name property. You probably intended to compare "student.name".