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 trialMatthew Francis
6,967 PointsNewbie - I'm lost in the "for:each loop" part...need aid clarifying
public String getCurrentProgress() {
String progress = "";
for (char letter: mAnswer.toCharArray()) {
char display = '-';
if (mHits.indexOf(letter) >= 0) {
display = letter;
}
progress += display;
}
return progress;
}
what I dont get is how does char display = '-' manage to censor the word "treehouse (mAnswer)" with "-", Im assuming it's something related on how progress and progress+=display works?
1 Answer
Zachary Betz
10,413 PointsHi Matthew.
So display
defaults to '-'
(we are assuming the user has not guessed correctly).
The value of display
is only changed if the user has guessed a correct letter. i.e. if the below condition evaluates to true
.
if (mHits.indexOf(letter) >= 0) {
display = letter;
}
If true
, the above code would assign the correctly guessed letter to display
.
Matthew Francis
6,967 PointsSo display defaults to '-' (we are assuming the user has not guessed correctly).
What I'm confused about is this part exactly! How does the computer know how many "display" it needs to be incremented to display ----------. From my understanding
public String getCurrentProgress() {
String progress = ""; //Stores the "progress" value;
for (char letter: mAnswer.toCharArray()) {
char display = '-';
if (mHits.indexOf(letter) >= 0) {
display = letter;
}
progress += display; //Increment "display" once; but how does the computer know how many times it should be incremented in order to make the value of treehouse into ----------?
}
return progress;//Returns the value of "progress";
}
Sam Deighton II
25,602 PointsThe computer already knows how long the word is because it knows the word(mAnswer). The function .toCharArray() seperates each letter into a spot in the array, and each of those spots has and an index(how far it is from the beginning of the word) The for:each loop runs until there is no more letters (the end of the mAnswer array and the last number in the index)
Matthew Francis
6,967 PointsThe for:each loop runs until there is no more letters (the end of the mAnswer array and the last number in the index) @Sam Deighton II
I TOTALY FORGOT ABOUT THIS ASPECT, EVEYRTHING MAKES SENSE NOW! THANK YOU EVERYONE!
Sam Deighton II
25,602 PointsSam Deighton II
25,602 PointsWe start off with all letters being "-", so "---------"
when we make a guess, the if statement is used used as a True/False statement for each index
the if statement says if a correct letter(mHits) is found replace the variable display(our '-') with that letter(our guess)
the progress string will then display that letter instead of the dashes.