jeudi 4 février 2021

Why doesn't my if statement respond to the changes I made to the variables?

var count = 0;    
if(count % 2 == 0) {
        for (let i = 0; i < squareStorage.length; i++) {
            squareStorage[i].addEventListener("click", function() {
                console.log("This is: " + i)
                console.log(count%2)
                count++;
                console.log(count);
            });
        }
    }
    
else if(count % 2 == 1) {
    console.log(count%2)
        for (let i = 0; i < squareStorage.length; i++) {
            squareStorage[i].addEventListener("click", function() {
            console.log("This is (no.2): " + i)
            count++;
            console.log(count);
        }, false);
    }
}

Those are part of my code. I wanted the variable count to change as I click on a bunch of divs which I threw into the squareStorage variable. I tried by increasing variable count whenever I click. However, my if statement somehow keeps on running, even when count % 2 is not 0.

Aucun commentaire:

Enregistrer un commentaire