I'm working on a hangman game in JavaScript and if the user guesses a letter correctly then I need to replace the "-" with the letter that the user has guessed in the corresponding position to where the letter is in the word. I can't seem to be able to replace the string, I can add the string on to the start or end of the "-"'s but when it comes to replacing them, I can't seem to figure out the solution. Any help appreciated. Thanks.
Function that checks if the letter is in the word or not
document.addEventListener("keydown", function textFunction(event)
{
if (event.keyCode > 64 && event.keyCode < 91)
{
var guess = event.keyCode;
var a = "a";
var letterGuess = String.fromCharCode(event.keyCode).toLowerCase();
if(sayings[randomSaying].indexOf(letterGuess) >= 0){
var letterLocation = sayings[randomSaying].indexOf(letterGuess) + 1;
progress.innerHTML += letterGuess;
} else {
alert("no");
}
alert(letterGuess);
}
else
{
alert("Please type a letter");
}
});
Prints out the dashes
for (var i = 0; i < sayings[randomSaying].length; i++)
{
progress.innerHTML += "-";
}
Variables
var progress = document.getElementById("dashes");
var sayings = [
"cash on the nail",
"charley horse",
"double cross",
"fit as a fiddle",
"hands down",
"if the cap fits",
"mumbo jumbo",
"see red",
"stone the crows",
"thick and thin",
]
sayings.toString();
var randomSaying = Math.floor(Math.random()*sayings.length);