//sample solution to Hangman Exercise 4
// 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);
// Use indexOf() again and store in checkLetter
checkLetter = answer.indexOf(letter, checkLetter+1);
}
// Return our string, modified or not
return shown;
}
//for testing purposes
console.log(guessLetter('a','______','banana'));
console.log(guessLetter('a','______','apples'));
console.log(guessLetter('a','______','futura'));
No comments:
Post a Comment