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 Java Objects Creating the MVP Storing Guesses

Aakash Srivastav
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 Points

String 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.

Gustavo Winter
Gustavo Winter
Courses Plus Student 27,382 Points

Hello 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.

Michael Werlinger
Michael Werlinger
1,259 Points

I 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
katke
5,731 Points

To 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.