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

Shouldn't this just work?

Something happens when a user is found, for some reason it doesn't push anything to "searchResult". Tried asking on the JS IRC, but nobody replied to my question -_-

function Student(name, track, achievements, points) {
    this.name = name;
    this.track = track;
    this.achievements = achievements;
    this.points = points;
}

var students = [],
    userSearch,
    searchResult;

students.push(new Student('Hamza', 'Full-stack Javascript developer', 40, 12000));
students.push(new Student('Cindy', 'Full-stack Javascript developer', 23, 9847));
students.push(new Student('Robert', 'Full-stack Javascript developer', 54, 21000));
students.push(new Student('John', 'Full-stack Javascript developer', 28, 10923));
students.push(new Student('Kim', 'Full-stack Javascript developer', 95, 50093));

while (true) {
    userSearch = prompt("Please enter the username of the student you're trying to find. Done with searching? Type \"quit\".");

    if (userSearch === null || userSearch.toLowerCase() === "quit") {
        break;
    } else {
        for (var i = 0; i < students.length; i++) {
            for (objKey in students[i]) {
                if (students[i][objKey].indexOf(userSearch) != -1) {
                    searchResult.push(students[i]); // nothing happens here
                    console.log("User found! " + searchResult);
                } else {
                    userSearch = "";
                    console.log("Student not found. Please try again.");
                }
            }
        }
    }
}
Simon Coates
Simon Coates
28,694 Points

The problems with your code are:

1) you're overwriting userSearch.

2) it didn't seem to like

for (objKey in students[i]) {
                if (students[i][objKey].indexOf(userSearch) != -1) {

as this calls indexof on some values that are numeric, not strings.

3) it seemed to not like calling .push on searchresults until i set searchresutls to []. Prior to this, i got an "undefined" error.

1 Answer

Simon Coates
Simon Coates
28,694 Points

i simplified your code a little (eliminated searching on all object attributes to avoid indexOf error) but the following seems to work for me:

function Student(name, track, achievements, points) {
    this.name = name;
    this.track = track;
    this.achievements = achievements;
    this.points = points;
}

var students = [],
    userSearch,
    searchResult;
var fields = ['name', 'track'];

students.push(new Student('Hamza', 'Full-stack Javascript developer', 40, 12000));
students.push(new Student('Cindy', 'Full-stack Javascript developer', 23, 9847));
students.push(new Student('Robert', 'Full-stack Javascript developer', 54, 21000));
students.push(new Student('John', 'Full-stack Javascript developer', 28, 10923));
students.push(new Student('Kim', 'Full-stack Javascript developer', 95, 50093));

var searchResult = [];
while (true) {
    var userSearch = prompt("Please enter the username of the student you're trying to find. Done with searching? Type \"quit\".");

    if (userSearch === null || userSearch.toLowerCase() === "quit") {
        break;
    } else {
        for (var i = 0; i < students.length; i++) {
                    if (students[i]['name'].indexOf(userSearch) != -1) {
                        searchResult.push(students[i]);
                        console.log("User found! " + searchResult);
                    }
            }
    }
}
Simon Coates
Simon Coates
28,694 Points

So this is what i tried next:

function Student(name, track, achievements, points) {
    this.name = name;
    this.track = track;
    this.achievements = achievements;
    this.points = points;
}

var students = [],
    userSearch,
    searchResult;
var fields = ['name', 'track'];

students.push(new Student('Hamza', 'Full-stack Javascript developer', 40, 12000));
students.push(new Student('Cindy', 'Full-stack Javascript developer', 23, 9847));
students.push(new Student('Robert', 'Full-stack Javascript developer', 54, 21000));
students.push(new Student('John', 'Full-stack Javascript developer', 28, 10923));
students.push(new Student('Kim', 'Full-stack Javascript developer', 95, 50093));

var searchResult = [];
while (true) {
    var userSearch = prompt("Please enter the username of the student you're trying to find. Done with searching? Type \"quit\".");

    if (userSearch === null || userSearch.toLowerCase() === "quit") {
        break;
    } else {
        for (var i = 0; i < students.length; i++) {
                  for(var y = 0; y< fields.length; y++)
                 {
                        if (students[i][fields[y]].indexOf(userSearch) != -1) {
                            searchResult.push(students[i]);
                            break;
                            console.log("User found! " + searchResult);
                        }//endif
                  }//endfor
            }//endfor
    }//end else
}//end while

Hope it helps. (it's 1.21 AM here so i can't really debug it further)