mardi 2 janvier 2018

Incrementing a variable 1 time in a loop for further checking with an if

So I have 2 questions regarding those 2 ifs in my code. So, firstly I want to check if fail is superior to 7, but it doesn't seem to work

If the value of the variable fail is superior to 7 it would increment its variable by 1.

The way it works is that there is more code behind it which sets up a word and the user/player would have to guess the word using letters.

If the letter guessed is equal to one of the word then it would replace it, if not it would increment the variable fail and then check if it is superior to 7 then its basicly game over:

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;
            }
        }
    }
}

It does increase the fail variable which is a global variable, doing that fail++ or fail = fail + 1, but it never reaches the if statement.

Other than that when the

if (dummy === word.charAt(i)) {
    letters[i] = dummy;
    //cs == 0 ? score1++ : score2++
}

is made it also increases the fail variable, and why is that?

I would appreciate if you could answer those 2 questions.

Aucun commentaire:

Enregistrer un commentaire