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

Nouh Ahmed
Nouh Ahmed
7,085 Points

STUDENT SEARCH CHALLENGE

The student search challenge is working but i added else part to print if user types a name that does not exist in the students object.

for ( var i = 0; i < students.length; i += 1) { student = students[i]; if ( student.name === search ) { message = getStudentData( student); print(message); } else if (student.name !== search) { message = ' is not in the list '; print(message); } } }

Lorenzo Pieri
Lorenzo Pieri
19,772 Points

Can you reformulate your question?

1 Answer

David Kanwisher
David Kanwisher
9,751 Points

There will be a problem because of the "Else if" statement that is inside your loop.

Let's say you have 4 objects in your array with the names "Tom", "Bob", "Carl", "John". Let's then say you search for the name "Carl" when prompted.

Your first loop will return "is not on the list" (Carl is not the first object name)

Your second loop will return "is not on the list" and overwrite the previous loop message (Carl is not the second object name)

Your third loop will display a "Carl" and his info and overwrite all previous loop messages (Carl is found!)

Your final loop will return "is not on the list" and overwrite all previous loop messages. (Carl is not the last object name)

So although Carl was found in the array, your message will say Carl is not on the list.