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

Java

Rafał Stasiak
Rafał Stasiak
3,763 Points

Why whe need to use "not not found" instead of just "found" (!= -1 / ==1)

Hello team,

Why we need to use "not not found" instead of just "found" (!= -1 / ==1)

for example

misses.indexOf(letter) == 1 || hits.indexOf(letter) == 1 ?

Java logic requires misses.indexOf(letter) != -1 || hits.indexOf(letter) != -1 to work properly but why?

I dont get this double negation

1 Answer

Steven Parker
Steven Parker
229,732 Points

The "not found" value is consistently -1, so it's easy to test for. But a "found" result could be any non-negative value, so it would not help to test for any specific one.

You do, however, have other options besides using the "not equals" operator:

misses.indexOf(letter) > -1 || hits.indexOf(letter) > -1   // or ">= 0"