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

Lee Howard
Lee Howard
6,621 Points

Extra credit help

Hey guys so I'm quite stuck on the extra credit part of the challenge. I added a final 'else' clause but all it does is print the persons name I wrote and says they aren't in the records, when they actually are..

Also. I have no idea how to check for the double name problem.

Here is my code so far.

var message = '';
var student; var questions; var search

var students = [ { name: "Lee", track: "JS", achievements: 5, points: 25 }, { name: "Stuart", track: "HTML", achievements: 7, points: 30 }, { name: "Dean", track: "CSS", achievements: 9, points: 35 }, { name: "Sam", track: "IOS", achievements: 11, points: 40 }, { name: "Denver", track: "Front", achievements: 13, points: 45 }, ];

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

function getStudentReport ( student ) { var report = '<h2>Student: ' + student.name + '</h2>'; report += '<h2>Track: ' + student.track + '</h2>'; report += '<h2>Achievements: ' + student.achievements + '</h2>'; report += '<h2>Points: ' + student.points + '</h2>'; return report; }

while (true) { search = prompt('Search student records:Type a name [Jody] (or type "quit" to end'); if ( search === null || search.toLowerCase() === 'quit' ) { break; } for (var i = 0; i < students.length; i +=1) { student = students[i]; if ( student.name === search ) { message = getStudentReport ( student ); print(message); } else { print(search + 'Is not in our records, sorry.'); } } }

1 Answer

Steven Parker
Steven Parker
231,275 Points

While inside the loop, the code is checking each student for a match. No matter which one you are looking for, most will not match; so that's not a good place to make a "not fond" determination.

But if you keep track of whether one was found or not (perhaps with a boolean value), then you could determine if none were found after the loop has finished.

Give that a try, and happy coding!