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

Attempt at extra challenges

This is my attempt at popping an alert box up when the user searches for a name that's not in the array.

while (true) {
    studentSearch = prompt("Search student records: Type a name [Jody] (or type 'quit' to end)");
    if ( studentSearch === null || studentSearch.toLowerCase() === "quit" ) {
        break;  
    }
    for (var i = 0; i < students.length; i += 1) {
    student = students[i]
    if ( student.name === studentSearch ) {
        message = getStudentReport( student );
        print(message);
        }
    }
    if ( student.name !== studentSearch ) {
        alert('Nope!');
    }
}

At the moment it pops up the message regardless of whether or not the name exists. I've tried moving the if statement inside the while loop but before the for loop too, and it says the name property is undefined. Any suggestions?

1 Answer

Allison Davis
Allison Davis
12,698 Points

Can you post all of your code? That will make it easier to see what you're working with.

My best guess, not being able to see the array of objects you're working with, is that you need to have the alert block inside the 'for' loop instead.

Do you use var student earlier in your code? If not, you need to define it with a 'var student' in that first line of the for loop.

Good luck!