【问题标题】:Why is my function not looping with no errors showing?为什么我的函数没有循环并且没有显示错误?
【发布时间】:2019-10-21 19:43:43
【问题描述】:

我正在学习一本关于 Javascript 的书籍教程,其中我应该只使用函数来编写一个刽子手游戏,我快要结束了,但我无法弄清楚这一点......由于某种原因,我的函数没有循环,具体来说,我的第一个字母/输入无法获得提示按摩,之后,我无法获得更多提示按摩..因为它应该是实现的循环...我知道这是不同的基本问题,但我就是无法通过这个。如果有人只是看看它,那就太好了......

我尝试使用“console.log”并将其放在“if”语句和“for”循环中,以查看循环是否正常工作,循环是否正常工作,但我的提示没有循环/重复...而且我不知道为什么... :(

var updateGameState = function (guess, word, answerArray) {
// Update answerArray and return a number showing how many
// times the guess appears in the word so remainingLetters
// can be updated


    for (var j = 0; j < word.length; j++) {
            if (word[j] === guess && answerArray[j] !== word[j]) {
                answerArray[j] = guess;
                remainingLetters--;

                };

    }; getGuess();
        console.log(answerArray);   


};

var remainingLetters = word.length;
var correctGuesses = updateGameState(guess, word, answerArray);

现在我只想让我的提示信息一直弹出这么久,以至于我可以输入我的字母/猜测...

----下面是.js和html文件....----

var pickWord = function(random) {
  // Return a random word
  return random[Math.floor(Math.random() * random.length)];

};

var word = pickWord(["fakultet", "zirafa", "trava", "labelo"]);
console.log(word);

var setupAnswerArray = function(word) {
  // Return the answer array
  var answerArray = [];
  for (i = 0; i < word.length; i++) {
    answerArray[i] = "_";
  }
  console.log(answerArray);
  return answerArray;
};
var answerArray = setupAnswerArray(word);

var showPlayerProgress = function(answerArray) {
  // Use alert to show the player their progress
  return alert(answerArray.join(" "));

};
showPlayerProgress(answerArray);

var getGuess = function() {
  // Use prompt to get a guess
  return (prompt("Guess a letter, or click Cancel to stop playing " + answerArray)).toLowerCase();
};
var guess = getGuess();
console.log(guess);

//               HERE IS THE PROBLEM
///*******************************************************
var updateGameState = function(guess, word, answerArray) {
  // Update answerArray and return a number showing how many
  // times the guess appears in the word so remainingLetters
  // can be updated
  for (var j = 0; j < word.length; j++) {
    if (word[j] === guess && answerArray[j] !== word[j]) {
      answerArray[j] = guess;
      remainingLetters--;
    };
  };
  getGuess();
  console.log(answerArray);
};

var remainingLetters = word.length;
var correctGuesses = updateGameState(guess, word, answerArray);
//*********************************************************


/* - code from the book

var showAnswerAndCongratulatePlayer = function (answerArray) {
// Use alert to show the answer and congratulate the player
};

var word = pickWord();
var answerArray = setupAnswerArray(word);
var remainingLetters = word.length;
while (remainingLetters > 0) {
showPlayerProgress(answerArray);
var guess = getGuess();
if (guess === null) {
break;
} else if (guess.length !== 1) {
alert("Please enter a single letter.");
} else {
var correctGuesses = updateGameState(guess, word, answerArray);
remainingLetters -= correctGuesses;
}
}
showAnswerAndCongratulatePlayer(answerArray);


*/
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Section 2: JavaScript Language Basics</title>
</head>

<body>
  <h1>Section 2: JavaScript Language Basics</h1>
</body>

<script src="script.js"></script>

</html>

【问题讨论】:

  • 你需要循环调用updateGameState(),直到猜出所有字母。
  • 如果你没有写循环,为什么你期望它循环?
  • 嗯,我已经写在函数里面了……
  • @Antonio 是的,但这仅用于在单词中循环字母以检查用户是否猜到了。您没有编写的循环是您需要的循环 - 以便不断要求更多猜测。请参阅下面的答案。

标签: javascript function loops


【解决方案1】:

你需要不断循环,直到剩余的字母数为0。现在你要求猜测,更新一次状态,然后再要求猜测并停止。

将请求猜测然后将状态更新到另一个函数的过程封装起来可能是有意义的。然后您可以继续调用该函数,直到用户获胜。我还将对函数等的所有调用(即代码的衬里部分)移到一个块中,这样就很清楚了。当它分散在各种功能中时,很难遵循程序的流程。

演示:

var pickWord = function(random) {
  // Return a random word
  return random[Math.floor(Math.random() * random.length)];
};

var setupAnswerArray = function(word) {
  // Return the answer array
  var answerArray = [];
  for (i = 0; i < word.length; i++) {
    answerArray[i] = "_";
  }
  console.log(answerArray);
  return answerArray;
};

var getGuess = function() {
  // Use prompt to get a guess
  return (prompt("Guess a letter, or click Cancel to stop playing " + answerArray)).toLowerCase();
};

var updateGameState = function(guess, word, answerArray) {
  // Update answerArray and return a number showing how many
  // times the guess appears in the word so remainingLetters
  // can be updated
  for (var j = 0; j < word.length; j++) {
    if (word[j] === guess && answerArray[j] !== word[j]) {
      answerArray[j] = guess;
      remainingLetters--;
    };
  };
  console.log(answerArray);
};

function play() {
  var guess = getGuess();
  console.log(guess);
  updateGameState(guess, word, answerArray);
}

var word = pickWord(["fakultet", "zirafa", "trava", "labelo"]);
console.log(word);
var answerArray = setupAnswerArray(word);
var remainingLetters = word.length;
while (remainingLetters > 0) {
  play();
}

alert("You win - congratulations");
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Section 2: JavaScript Language Basics</title>
</head>

<body>
  <h1>Section 2: JavaScript Language Basics</h1>
</body>

<script src="script.js"></script>

</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-09
    • 2015-02-06
    • 2017-01-31
    • 2021-04-20
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 2020-05-11
    相关资源
    最近更新 更多