gpt4 book ai didi

javascript - 为什么我的函数没有循环且没有错误显示?

转载 作者:行者123 更新时间:2023-11-30 19:10:49 25 4
gpt4 key购买 nike

我正在阅读一本关于 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>

最佳答案

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

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

演示:

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>

关于javascript - 为什么我的函数没有循环且没有错误显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58493131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com