所以我对我的代码中的2个ifs有2个问题。 所以,首先我要检查失败是否优于7,但它似乎不起作用
如果变量fail
的值高于7,则会将其变量增加1.
它的工作方式是它背后有更多代码设置一个单词,用户/玩家必须使用字母猜测单词。
如果猜到的字母等于其中一个单词那么它会替换它,如果不是它会增加变量fail
,然后检查它是否优于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
变量,为什么会这样?
如果你能回答这两个问题,我将不胜感激。