I’m trying to build a hangman game using instructions from codecademy, but I’m having a hard time following alone. In the guessLetter function below, I’ve added two lines of code following the instructions they gave me
This
shown = alterAt(checkLetter,letter,shown); ///this is mine
and this
checkLetter = indexOf(letter, checkLetter +1);
However, I can’t get the tests to pass. I’d be grateful if you could assist.
// Skip to guessLetter(). You shouldn't need to touch this function.
function alterAt ( n, c, originalString ) {
return originalString.substr(0,n) + c + originalString.substr(n+1,originalString.length);
}
function guessLetter( letter, shown, answer ) {
var checkLetter = -1; // This variable will hold the indexOf()
checkLetter = answer.indexOf(letter); // Single Argument Version starting at 0
while ( checkLetter >= 0 ) {
// Replace the letter in shown with alterAt() and then store in shown.
shown = alterAt(checkLetter,letter,shown); ///this is mine
// Use indexOf() again and store in checkLetter
checkLetter = indexOf(letter, checkLetter +1); ///this is mine
}
// Return our string, modified or not
return shown;
}
Update
If the guess is correct, it’s supposed to return shown modified by the guess. For example, if the word is tree and the player guesses ‘e’, it should return ‘__ee.’ Here’s a fiddle I can’t get to work using the word ‘whatever’: http://jsfiddle.net/mjmitche/YAPWm/2/
You have an error on this line:
The
.indexOf()method needs to be called on a string like it was on the line before thewhile(). Change it to this:Working version: http://jsfiddle.net/YAPWm/1/