所以我对我的代码中的2个ifs有2个问题。 所以,首先我要检查失败是否优于7,但它似乎不起作用:
function doGuessWord() {
const letters = document.getElementById("dword").textContent.split(' ');
for(i = 0; i < wLength; i++){
if(dummy === word.charAt(i)) {
letters[i] = dummy;
//cs == 0 ? score1++ : score2++
}
else {
fail = (fail + 1)
console.log(fail);
break;
}
}
document.getElementById("dword").textContent = letters.join(' ');
if (letters.join('') == word) {
let player = ""
cs == 0 ? score1++ : score2++
cs == 0 ? player = player1: player = player2
$("input").hide();
$("input.wordi").show()
$("input.bi").show()
document.getElementById("dword").textContent = ""
document.getElementById("chrts").textContent = ""
alert("Congratulations, " + player + " won, the word was " + word + ".")
F.gword.value = ""
for(i = 0; i <= 30; i++) {
window["cl" + alp[i]] = true;
}
if (fail > 7) {
let player = ""
cs == 0 ? player = player2: player = player1
$("input").hide();
$("input.wordi").show()
$("input.bi").show()
document.getElementById("dword").textContent = ""
document.getElementById("chrts").textContent = ""
alert("Too Bad, " + player + " you lose, the word was " + word + ".")
F.gword.value = ""
for(i = 0; i <= 30; i++){
window["cl" + alp[i]] = true;
}
}
}
}
它确实增加了fail
变量,它是一个全局变量,执行fail++
或fail = fail + 1
,但它永远不会到达if语句。
除此之外
if (dummy === word.charAt(i)) {
letters[i] = dummy;
//cs == 0 ? score1++ : score2++
}
使它也增加了fail
变量,为什么会这样?
如果你能回答这两个问题,我将不胜感激。