因此,我的程序将随机单词打印到控制台,以便我可以检查该单词是用于测试和实现的,因此当我测试代码并猜测正确的正确字母时,单击猜测按钮后单词占位符不会更新只是循环通过刽子手的阶段。这是我的程序代码;
var wordsArray = ["monitor", "program", "application", "keyboard", "javascript", "gaming", "network"];
var word;
var goesLeft;
var placeholder;
var input;
var wordLength;
var wordSubstring;
var currentWord;
function newGame()
{
placeholder = "";
goesLeft = 10;
var word = wordsArray[Math.floor(Math.random() * wordsArray.length)];
currentWord = 0;
word = wordsArray[currentWord];
wordLength = currentWord.length;
wordSubstring = currentWord.substring;
console.log(word);
var myElement = document.getElementById("button").innerHTML = "Click to guess";
var myPicElement = document.getElementById("hangimage").src = "http://fetlar.kingston.ac.uk/pp/hangman10.jpg";
for (var count = 0; count < word.length; count++)
{
placeholder = placeholder + "-";
}
document.getElementById("placeholder").innerHTML = placeholder;
document.getElementById("gamestatus").innerHTML = "Game running";
}
function guessLetter()
{
var correct = 0;
var inputBox = document.getElementById("guessinput");
input = inputBox.value;
console.log(word);
for (var count = 0; count < wordLength; count++)
{
if (input == word.substring(count, count + 1))
{
correct++;
placeholder = placeholder.substring(0, count) + input + placeholder.substring(count + 1, placeholder.length + 1);
document.getElementById("placeholder").innerHTML = placeholder;
}
}
if (correct == 0)
{
goesLeft--;
}
var url = document.getElementById("hangimage").src = "http://fetlar.kingston.ac.uk/pp/hangman" + goesLeft + ".jpg";
if (placeholder == word)
{
document.getElementById("hangimage").src = "http://fetlar.kingston.ac.uk/pp/hangman_win.jpg";
alert("You guessed the word correctly. You win!");
}
if (goesLeft == 0)
{
alert("You lose");
newGame();
}
}
newGame();
document.getElementById("button").onclick = guessLetter;
这是我的占位符代码,问题必须存在,程序也运行良好,我没有收到任何错误。
for (var count = 0; count < wordLength; count++)
{
if (input == word.substring(count, count + 1))
{
correct++;
placeholder = placeholder.substring(0, count) + input + placeholder.substring(count + 1, placeholder.length + 1);
document.getElementById("placeholder").innerHTML = placeholder;
}
}
这是链接到代码的 HTML;
<div style="text-align: center;">
<h1>Hangman</h1>
<img id="hangimage" src="http://fetlar.kingston.ac.uk/pp/hangman9.jpg" />
<div id="placeholder" style="font-size: 1.5em; font-family: monotype; color: red;">-------</div>
<div id="gamestatus">Game running</div>
<input id="guessinput" type="text" size="1" />
<button id="button">Click to guess</button>
<br/>
<div>Guessed Letters</div>
<div id="guessedletters">(guessed letters will go here)</div>
</div>
如果有人能告诉我问题出在哪里,那就太好了,在此先感谢。