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

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.