在这种情况下如何检查?

时间:2018-01-01 13:21:18

标签: javascript html

所以我对我的代码中的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变量,为什么会这样?

如果你能回答这两个问题,我将不胜感激。

0 个答案:

没有答案