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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Aakash Srivastav](https://secure.gravatar.com/avatar/67d42ff92ed9bcb21fe7cabeb710965c?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsString concatenation
Why hits ans misses are used??? Please explain in detail. I am given lots of time understanding this program but still struggling. Please help me as soon as possible.
![Michael Werlinger](https://secure.gravatar.com/avatar/1217caf46da30c3ea84db9c90ec81c35?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Michael Werlinger
1,259 PointsI am with Aakash on this one, I mean to use a negative creating a double negative just radically increases the chance of making a mistake. I am not understanding why we would want to use it in this case.
lets grab an example direct from the video here
public boolean applyGuess(char letter) {
boolean isHit = answer.indexOf(letter) != -1; // HERE!!!! This!!!! What the ! Logic
if(isHit) {
hits += letter;
}else {
misses += letter;
}
return isHit;
}
Creating the double negative here makes as much sense to me as adding a screen door to a submarine!!! Why do it?!?!?! Why not just use the "K.I.S.S." philosophy and say >= 0 ?????
Actually now that I am thinking of this, the above bit is saying that...
IF - guess is wrong - true - add char to hits string - else if false - add char to misses.
Can someone please explain this in detail as it makes zero logic.
1 Answer
![katke](https://uploads.teamtreehouse.com/production/profile-photos/198122/micro_1484080_10100361411325934_2032725498_o.jpg)
katke
5,731 PointsTo answer your Q Michael, you certainly could do it the way you're describing, so it would become:
boolean isHit = answer.indexOf(letter) >= 0;
That boils down to the same thing. I think it just comes down to a matter of preference. In many programming languages, the idea of -1 meaning the item is not present in a string/array is a pretty common convention, so for someone like Craig who's been at this a while, structuring it that way probably feels like second nature. But I can see how it could feel unnecessarily complicated from other perspectives. For the rest of your post:
Actually now that I am thinking of this, the above bit is saying that... IF - guess is wrong - true - add char to hits string - else if false - add char to misses. Can someone please explain this in detail as it makes zero logic.
You've got it flipped; whenisHit
is true, it means the guess was correct. If the outcome of the indexOf
check return anything other than -1 then we know the guess was a good one and that letter is present somewhere in the answer string.
Gustavo Winter
Courses Plus Student 27,382 PointsGustavo Winter
Courses Plus Student 27,382 PointsHello Aakash,
Hits ands misses are used to check with the letter is correct or not, in other words if the letter is in the answer or not.
Let's use "treehouse" like a example:
If your hint is the letter "A" what is not inside "treehouse", the variable misses will append it to her.
But if you try to hint the letter "e" what is inside "treehouse" that will be append to hits and the places where the letter appears will be filled.