Have a basic hangman game here. Seems to be working, but I can't get duplicate character guesses to work properly. There are only two words in the bank now, "popcorn" and "apples". The first time you guess "p" for apples it fills in the first p, but won't fill in the second.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hangman!</title>
</head>
<body>
<h1>Hangman</h1>
<h2>Press a letter to guess.</h2>
<div id="secret"> </div>
<script>
var computerChoices = ['apples', 'popcorn'];
var progress = "";
// This chooses a word from the set randomly.
var secretWord = computerChoices[Math.floor(Math.random() * computerChoices.length)];
for (i=0; i < secretWord.length; i++){
progress += "_";
}
// When the user presses the key it records the keypress and then sets it to userguess
document.onkeyup = function(event) {
var letter = String.fromCharCode(event.keyCode).toLowerCase();
if (secretWord.indexOf(letter) > -1){
console.log("Good Guess!");
index = secretWord.indexOf(letter);
progress = progress.substr(0, index) + letter + progress.substr(index + 1);
// Placing the html into the secret ID
document.querySelector('#secret').innerHTML = progress;
if ((/([a-zA-Z]).*?\1/).test(secretWord)) {
console.log("Yep, there's a duplicate here")
}}else{
console.log ("Eeeeeennnnnghh! Wrong! Try again dumbassss!");
}
}
</script>
</body>
</html>