여기에 행맨 게임이 있습니다. 일하는 것 같지만, 중복 된 캐릭터 추측을 올바르게 할 수는 없습니다. 현재 은행에는 "팝콘"과 "사과"라는 단어가 두 개뿐입니다. 처음으로 사과를 "p"로 추측하면 첫 번째 p를 채우지 만 초를 채우지는 않습니다.문자열의 중복 문자를 바꾸는 방법 - Javascript
<!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>