我目前正在为我的编码课程开展一个项目,而且我的代码存在问题。我的代码是在wrongLetters数组中添加正确的字母,我不确定原因。它也只对第二个正确的字母和向前输入执行此操作,这意味着如果我正确地猜到了一个正确的字母,它会将它添加到correctLetters数组,但是之后每个正确的字母都会导致它被添加到correctLetters和wrongLetters中什么时候应该只添加到correctLetters。如果你可以帮忙,我很困惑。谢谢。
var guessWords = ["school", "test", "quiz", "pencil", "ruler", "protractor", "teacher", "homework", "science", "math", "english", "history", "language", "elective", "bully", "grades", "recess", ]
var secretWord = guessWords[Math.floor(Math.random()*guessWords.length)];
var wrongLetters = []
var correctLetters = []
var repeatLetters = []
function startGame() {
var testWord = document.getElementById("randTest").innerHTML = secretWord;
var correctLettersOUT = "";
document.getElementById("currentGuess").innerHTML = secretBlanks(secretWord)
function secretBlanks(secretWord) {
for (var i = 0; i < secretWord.length; i++) {
correctLettersOUT+=("_ ");
} return correctLettersOUT;
}}
function correctWord() {
var guessLetter = document.getElementById("guessLetter").value;
document.getElementById("letter").innerHTML = guessLetter;
for (i=0; i < secretWord.length; i++) {
if (correctLetters.indexOf(guessLetter) === -1)
if (guessLetter === secretWord[i]) {
correctLetters.push(guessLetter);
} else if(wrongLetters.indexOf(guessLetter) === -1) {
wrongLetters.push(guessLetter);
}
}
console.log(correctLetters);
console.log(wrongLetters);
console.log(repeatLetters);
}
答案 0 :(得分:0)
请参阅for循环中的条件 - 在第一次迭代中,如果字母与密码的第一个字母不同并且wrongLetters
中不存在,则将其添加到此数组中。
首先检查密码是否包含字母,然后决定哪个数组应包含此字母。 (顺便说一句。为什么在使用indexOf
或ES6 includes
函数时可以循环检查?)
答案 1 :(得分:0)
var guessWords = ["school", "test", "quiz", "pencil", "ruler", "protractor", "teacher", "homework", "science", "math", "english", "history", "language", "elective", "bully", "grades", "recess", ]
var secretWord = guessWords[Math.floor(Math.random()*guessWords.length)];
var wrongLetters = []
var correctLetters = []
var repeatLetters = []
function startGame() {
var testWord = document.getElementById("randTest").innerHTML = secretWord;
var correctLettersOUT = "";
document.getElementById("currentGuess").innerHTML = secretBlanks(secretWord)
function secretBlanks(secretWord) {
for (var i = 0; i < secretWord.length; i++) {
correctLettersOUT+=("_ ");
} return correctLettersOUT;
}}
function correctWord() {
var guessLetter = document.getElementById("guessLetter").value;
document.getElementById("letter").innerHTML = guessLetter;
for (var i = 0; i < secretWord.length; i++) {
if (correctLetters.indexOf(guessLetter) === -1)
if (guessLetter === secretWord[i]) {
correctLetters.push(guessLetter);
break;
}
}
if(wrongLetters.indexOf(guessLetter) === -1 && correctLetters.indexOf(guessLetter) === -1) {
wrongLetters.push(guessLetter);
}
console.log(correctLetters);
console.log(wrongLetters);
console.log(repeatLetters);
}
问题在于,在wrongLetters
数组中添加字母的代码位于正在检查正确字母的for循环内部,因此如果正确的字母让我们说字母2,则循环将检查字母1和然后将其添加到wrongLetters
数组中,因为它不是同一个字母。
我将支票移到了循环外面,现在字母被添加到正确的数组中。现在,如果你猜多于同一个字母,那么一旦它多次被添加到correctLetters数组,
如果你不想要那个行为,只需改变第一个,如果从
检查 if (guessLetter === secretWord[i])
到
if (guessLetter === secretWord[i] && correctLetters.indexOf(guessLetter) === -1)