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.

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge Solution

William Monteith
William Monteith
1,484 Points

Why "< 0" is required instead of "!=="?

I'm putting together a simple solution for the bonus part of if a student cannot be found in the directory. If I use the "student.name !== search", it only recognized "Trish" as a student enrolled, but no other (Dave, Jody, etc). However, if I use "student.name < 0" to looks for a -1, it functions. Why would the !== alter the output of what is searchable?

// This only pulls Trish

while (true) { search = prompt("What student do you want to search for? [Jody] (or type 'quit' to end)"); if (search === null || search.toLowerCase() === 'quit') { break; } for (var i = 0; i < students.length; i += 1) { student = students[i]; if (student.name !== search) { message = "That student isn't enrolled here" print(message); } else if (student.name === search) { message = getStudentReport(student); print(message); } } }

// This works, but I would've thought the code would be equivalent

while (true) { search = prompt("What student do you want to search for? [Jody] (or type 'quit' to end)"); if (search === null || search.toLowerCase() === 'quit') { break; } for (var i = 0; i < students.length; i += 1) { student = students[i]; if (student.name < 1) { message = "That student isn't enrolled here" print(message); } else if (student.name === search) { message = getStudentReport(student); print(message); } } }

Note: I'm only posting the while loop since the code higher up all matches the instructor's

2 Answers

The first loop 'works' for Trish because she is the last name in the loop. The loop goes through each name and since 'Dave' for example doesn't equal 'Trish' on the final iteration the if (student.name !== search) condition is met and that code executes.

The if statement code for the second loop 'if (student.name < 0)' doesn't execute for anyone. Note: you have 'if (student.name < 1).` I tried Jimmy and the page was blank. That condition doesn't check the name that was entered in the search. That statement is checking if the student name in students is less than an integer.