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 trialChris Tate
5,522 PointsA proposed solution for the Duplicate Student Record Search challenge
Here's my solution for this challenge. I pulled the for/if out of the while and made it a function. Would love feedback!
var message = '', student, search, notFound = true;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
function searchResults (obj) {
var report = '<h2>Student: ' + obj.name + '</h2>';
report += '<p>Track: ' + obj.track + '</p>';
report += '<p>Points: ' + obj.points + '</p>';
report += '<p>Achievements: ' + obj.achievements + '</p>';
return(report);
}
function searchStudents(arr, searchedName){
message = '';
notFound = true;
for (var i = 0; i < arr.length; i += 1) {
studentObj = arr[i];
if(studentObj.name.toLowerCase().indexOf(searchedName.toLowerCase()) > -1 ){
message += searchResults(studentObj);
notFound = false;
}
}
if(notFound){
message = '<h2>Sorry but we couldn\'t find a student by the name of ' + searchedName + '. Please try again.</h2>';
}
return message;
}
while (true){
search = prompt('Which student are you searching for? Please enter their name or type \'quit\' to exit');
if(search === null ||search.toLowerCase() === 'quit' ) {
break;
} else {
print(searchStudents(students, search));
}
}
2 Answers
Dylan Cairns
11,191 PointsNice code but I noticed that there's a bug in it. If you type any letter that is inside the names the indexof will still give a record.
Like if there was a student named Mario and you just type "a" into the search box it will bring up Mario's record. The only way to get a 'sorry we couldn't find...' message with this code is to type in a letter that isn't in any name.
Anyone know how to get around something like this and make indexOf only work for the full search?
audreysalerno
4,289 PointsSee my fix below
audreysalerno
4,289 PointsAddressing the bug found by Dylan:
In the searchStudents() function,
studentObj.name.toLowerCase().indexOf(searchedName.toLowerCase()) > -1
is looking for the index of the string value of searchedName
in within the string value of the name
property. Therefore, it is working as directed, since, in the example of someone inputting "a" into the prompt, the index of "a" for a student named "Mario", is > -1. The use of indexOf() in this situation is not what we want, because we don't want to look for the position of the requested string within the string value of each name - we want to check if the requested name is equal to each student's name.
The following code fixes the bug with a modification of searchStudents()
to check if studentObj.name
is equal to the provided searchedName
:
function searchStudents(arr, searchedName){
message = '';
notFound = true;
for (var i = 0; i < arr.length; i += 1) {
studentObj = arr[i];
//check if studentObj.name is equal to the provided searchedName
if(studentObj.name.toLowerCase() === searchedName.toLowerCase()){
message += searchResults(studentObj);
notFound = false;
}
}
if(notFound){
message = '<h2>Sorry but we couldn\'t find a student by the name of ' + searchedName + '. Please try again.</h2>';
}
return message;
}
shaunboleh
14,970 Pointsshaunboleh
14,970 PointsI added your script and put it to use - works for me - shows multiple people with the same name. I will study it further for learning purposes :)
I hope you can get some more enlightened feedback from someone else.