I am a beginner to JavaScript, writing a hangman-type game where a user enters a string answer, then enters one character at a time to try and guess the answer. (I'm changing it later to use an array of set answers instead of a user entered one, this way is just for testing). However, when I try to loop through my answer array and compare it to the input character in the function answerFunction(), it sets my whole blank array the same as the answer array instead of one character at a time as I want.
<html>
<head>
<script src="hangman.js"></script>
</head>
<body>
<h2>Hangman Game</h2>
<p>Game Description</p>
<input id="userinput" placeholder="Input Answer"></input>
<button value='send' id="submit" onclick="userInput()">Submit</button><br><br>
<input id="userguess" placeholder="Input Guess"></input>
<button value='send' id="submit" onclick="userGuess()">Submit</button>
<p id="answer-placeholder"></p>
<p id="guess-placeholder"></p>
</body>
</html>
function userInput() {
inputAnswer = document.getElementById("userinput").value;
answerArray = inputAnswer.split("");
blankArray = answerArray.slice(0);
blankArray.fill("_ ");
document.getElementById("answer-placeholder").innerText = blankArray;
}
function userGuess() {
inputChar = document.getElementById("userguess").value;
answerLoop();
}
function answerLoop() {
for (var i = 0; i < answerArray.length; i++) {
if (inputChar = answerArray[i]) {
blankArray[i] = answerArray[i];
document.getElementById("guess-placeholder").innerText = blankArray;
} else {
break;
}
}
}
Example: Answer is John I type in J, and it displays J,o,h,n instead of the expected J,,,_