vendredi 28 juillet 2017

Javascript picking few elements from array

I want to check if an array includes clicked elements. So the condition should be fulfilled if the array contains three clicked elements(1,2,3 or 4,5,6 or, 7,8,9) but it doesn't work. The console displays "You won..." when i click third or sixth or ninht element. Syntax problem?

for (i=1;i<=9;i++) {
  functionOnclick(i){
    array.push("element" + i + "clicked");

    if(
    (array.includes(element1clicked&&element2clicked&&element3clicked)) ||
    (array.includes(element4clicked&&element5clicked&&element6clicked)) ||
    (array.includes(element7clicked&&element8clicked&&element9clicked)))

       {console.log("You won the game!"}

  }

}

Edited:

<script>

    var i="";
    var a="O";
    var tablica1=[""];

    for (i=1;i<=9;i++) {    

        function Klik(i) { 

                if (a=="X") {
                    a="O";
                    document.getElementById("pole"+i).innerHTML=a; 
                    document.getElementById("pole"+i).className=("active1");
                    document.getElementById("gracz").innerHTML="X, Click!";
                    document.getElementById("pole"+i).removeAttribute("onclick"); 
                }

                else if (a=="O") {
                    a="X";
                    document.getElementById("pole"+i).innerHTML=a;
                    document.getElementById("pole"+i).className=("active2");
                    document.getElementById("gracz").innerHTML="O, Click!";
                    document.getElementById("pole"+i).removeAttribute("onclick"); 
                    tablica1.push("pole"+i+"active2");
                }
                console.log(tablica1);

                if (
                (tablica1.includes("pole1active2"&&"pole2active2"&&"pole3active2"))
                ||
                (tablica1.includes("pole4active2"&&"pole5active2"&&"pole6active2"))
                ||
                (tablica1.includes("pole7active2"&&"pole8active2"&&"pole9active2"))
                )

                {console.log("won");}
        }       
    }


    </script> 

Aucun commentaire:

Enregistrer un commentaire