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

Philipp Henk
Philipp Henk
12,494 Points

*Noob* Need some help with the output of the students' data

each time I type in the prompt dialogue box the name 'Dave', which obviously exist in the object lateral I get the failure alert, that 'Dave' couldn't be found.

Does anybody know the reason, why the request in my code does not work?

https://w.trhou.se/08x2iimplv

1 Answer

Steven Parker
Steven Parker
229,732 Points

The main issue is this line:

        if (search !== student.name || search !== 'quit') {

Since these two tests are combined using "OR" logic ("||"), the alert will be given any time either test is true. And when the search term is "Dave", it is not "quit", so that will produce an alert.

Then, this is being performed inside the search loop, which means the alert will be issued again and again, once for every student record in the list (whether it matches or not).

What you might want to do is to have a boolean variable that you can set when a match is found, and then perform the test for the alert after the loop finishes, looking at only that boolean value.