Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Chris Tate
Chris Tate
5,522 Points

A 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));
    }
}

I 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.

2 Answers

Dylan Cairns
Dylan Cairns
11,191 Points

Nice 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?

See my fix below

Addressing 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;
}