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 trial

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

Madeline Yao
seal-mask
.a{fill-rule:evenodd;}techdegree
Madeline Yao
Full Stack JavaScript Techdegree Student 9,611 Points

Everything looks fine, but with my code, I cannot find the person and I do not know why

A. The code of search_report.js on js folder: var keyword;

while(true) { keyword = prompt('Please type the name of the person you are looking for'); if(keyword === 'quit'){ break;

} else {
    if(find_figure(keyword)){
        var message = print_figure(keyword); 
        print(message); 

    } else {
        keyword = prompt('Who you are looking for is not available in our database, please try                again'); 
    }

}

}

function find_figure(keyword){ for(var i = 0; i< students.length; i +=1){ if(keyword === students[i].name){ return true;

    }

}
return false; 

}

function print_figure(keyword){ var message = ''; message += '<h2>Name: ' + keyword.name + '</h2>'; message += '<p>Track: ' + keyword.track + '</p>'; message += '<p>Achievements: ' + keyword.achievements + '</p>'; message += '<p>Points: ' + keyword.points + '</p>'; return message;

}

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

}

B. The code of students.js on js folder: var students = [ { name: 'Dave', track: 'Front End Development', achievements: 158, points: 14730 }, { name: 'Jody', track: 'iOS Development with Swift', achievements: '175', points: '16375' }, { name: 'Jordan', track: 'PHP Development', achievements: '55', points: '2025' }, { name: 'John', track: 'Learn WordPress', achievements: '40', points: '1950' }, { name: 'Trish', track: 'Rails Development', achievements: '5', points: '350' } ];

C. The code of student_report.js on js folder: var message = ''; var student;

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

for (var i = 0; i < students.length; i += 1) { student = students[i]; message += '<h2>Student: ' + student.name + '</h2>'; message += '<p>Track: ' + student.track + '</p>'; message += '<p>Points: ' + student.points + '</p>'; message += '<p>Achievements: ' + student.achievements + '</p>'; }

Above are all of the codes I have for the challenge in js folder, I ran the code but I cannot search the person I want. Could anyone please help me look for mistakes I made? Thank you. Help would be appreciated.

1 Answer

Steven Parker
Steven Parker
229,732 Points

The function "print_figure" is defined to take a "keyword" argument, and when it is called a string is passed to it. But it attempts to access properties that a string would not have. It seems obvious that one of the objects from the "students" array should be passed to it instead.

The function that identifies the record only returns a true/false condition, so a good bit of logic re-design will be needed to display the record that matches the search term.