我遵循本教程的一本书,如果猜测不正确,我尝试添加最后的猜测。在开始时,我将这个变量设置为4.简单的hang子手游戏尝试次数不起作用
现在我的猜测无 - 减少到许多数字一次所以我不知道如何解决它只减少一个尝试从4,直到猜测Nr < 0. 可以有人请告诉我该怎么做?
var words = [
\t "javascript",
\t "monkey",
\t "amazing",
\t "pancake"
\t ];
var word = words[Math.floor(Math.random() * words.length)];
//create an empty array called answerArray and fill it
//with underscores (_) to match the number
//of letters in the word
var answerArray = [];
for(var i=0; i < word.length; i++) {
\t answerArray[i] = "_";
}
//every time the player guesses a
//correct letter, this value will
//be decremented (reduced) by 1
var remainingLetters = word.length;
var guessesNr = 4;
while((remainingLetters > 0) && (guessesNr > 0)) {
\t // Show the player their progress
\t alert("The word is from " + word.length + " letters " + answerArray.join(" "));
\t // Take input from player
\t var guess = prompt("Guess a letter");
\t // if the player clicks the Cancel button, then guess will be null
\t if(guess===null) {
\t \t // break to exit the loop
\t \t break;
\t \t //ensuring that guess is exactly one letter
\t } else if(guess.length !== 1) {
\t \t alert("Please enter a single letter!");
\t } else {
\t \t for(var j = 0; j < word.length; j++) {
\t \t \t if(word[j] === guess) {
\t \t \t \t // sets the element at index j, from word,
\t \t \t \t // of answerArray to guess
answerArray[j] = guess;
\t \t \t \t remainingLetters--;
\t \t \t
\t \t \t } else { \t
\t \t \t \t //guessesNr--;
\t \t \t \t //console.log("guessesNr", guessesNr);
\t \t \t }
\t \t }
\t }
\t
}
//alert(answerArray.join(" "));
alert("Great job! The answer was " + word);
<!DOCTYPE html>
<html>
<head>
\t <title></title>
</head>
<body>
<script type="text/javascript" src="hangman-test1.js"></script>
</body>
</html>
我这样做,但即使我把正确的信仍然没有 - 就像我把错误的字母。你能检查我的代码吗? –
好的。如果他们没有得到任何匹配,我编辑的解决方案只会减少。 – rasmeister