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

Philip Kroupoderov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Philip Kroupoderov
Front End Web Development Techdegree Graduate 21,641 Points

null in if statement does not work

For the student record challenge at the very end of the JS loops, arrays and objects course only the null in my if statement is not working. Here's a link to my work:

https://w.trhou.se/hycddjp9zg

The problem is when I type a student that exists and then type 'quit', that student's record is written to the page. When I do the same thing but instead of typing 'quit' I click the cancel button, that student's record does not get written to the page. Can someone please explain me why, and how to make it work?

Steven Parker
Steven Parker
231,261 Points

You can't share a direct URL to your workspace, it's temporary and only exists while you are using it. But you can use the snapshot function in the workspace and provide the link to that.

1 Answer

Steven Parker
Steven Parker
231,261 Points

A null has no "toLowerCase" method, so when "search" is null, applying that method causes an error and the program ends before reaching the "print" statement.

You can fix this by restructuring the code so the method is only applied after the variable is determined to not contain null:

  search = prompt("Type a name");
  if (search === null || search.toLowerCase() === "quit") {
Philip Kroupoderov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Philip Kroupoderov
Front End Web Development Techdegree Graduate 21,641 Points

Steven thank you, it does work. I have tried the exact same thing but the search.toLowerCase() === quit condition came before the || operator. When that was the case it did not work. Could you also explain why? Does it really matter what comes first in an if statement when a logical operator is involved ??

Steven Parker
Steven Parker
231,261 Points

The OR operator (||) has what is called "left-to-right associativity". That means the expression on the left will be evaluated first. It also has what is called "short-circuit evaluation" which means that if the expression on the left evaluates as "truthy" then the expression on the right is ignored.

So if you test for null first, then when the value is null the "toLowerCase" method is never used. But when the tests are reversed, the method is applied to the null which causes an error and ends the program.

Happy coding!