I'm trying to make a Hangman game with Javascript, where the user enters a word for another person to guess. The word is sent to a function (makeUnderlines) and is made into an array of underlines and returned to the player with a prompt to enter a letter that they want to guess. The letter and the word is sent to a function that finds the positions of the letters (getPosition) in the word and sends the positions to another function. The function (insertLetters) is supposed replace the underlines with the letter at the positions given. All of this works except that the function doesn't replace anything in the array. It only replaces the first letter if it only occurs once in the word. Any advice to solving this would be greatly appreciated. I would also like to know if there is a way to prevent the user to entering a number in a word or as a guess.
<!DOCTYPE HTML>
<HTML>
<HEAD>
<META CHARSET="UTF-8">
<TITLE>Hangman</TITLE>
</HEAD>
<BODY>
<SCRIPT TYPE="text/javascript">
function makeUnderlines(word) {
for(i = 0; i < word.length; i++){
underlines.push("\"_\"");
}
return underlines;
}
function getPositions(word, letter) {
for(var i=0; i< word.length;i++) {
if (word[i] === letter) positions.push(i);
}
insertLetters(underlines, positions, letter)
}
function insertLetters(underlines, positions, bokstav){
underlines[positions] = letter;
return underlines;
}
let word = prompt("Choose a word for the player!");
underlines = [];
positions = [];
makeUnderlines(word);
for(i = 7; i > 0; i--){
letter = prompt("["+underlines+"]\n Guess a letter. You have " + i + " guesses left");
getPositions(word, letter);
}
</SCRIPT>
</BODY>
</HTML>