JSFiddle - React, Tailwind, and code Playground
CSS
body {font-family: "Courier New"; font-size: 12px;}
JavaScript
function alterAt ( n, c, originalString ) {
return(originalString.substr(0, n) + c + originalString.substr(n + 1));
}
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 = answer.indexOf(letter, checkLetter +1); ///this is mine
}
// Return our string, modified or not
return shown;
}
document.body.innerHTML = guessLetter("o", "_____", "moose");
document.body.innerHTML = guessLetter("o", "_____", "moose");