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.
Ivana Lescesen19,442 Points
Is it possible to do this task by using a while loop and indexOf() method?
Amazing people at treehouse
Is it possible to do this task by using a
while loop and
And if not how can we check if the
search = prompt("Search for a student. Type a name of a student or 'quit' to exit");
is equal to
Thank you so much :)
Steven Parker217,570 Points
There's two issues with using indexOf in this situation.
- You would have to provide indexOf with the entire student object to match on, not just the name.
- indexOf only finds the first match, but the sample method finds all that have the same name.
Instead of indexOf() you can use the .filter() method on your array that calls a function you create. It then passes each item/object from your array to your function where you should evaluate each item to true or false. For instance: