Javascript picking few elements from array -


i want check if array includes clicked elements. condition should fulfilled if array contains 3 clicked elements(1,2,3 or 4,5,6 or, 7,8,9) doesn't work. console displays "you won..." when click third or sixth or ninht element. syntax problem?

for (i=1;i<=9;i++) {   functiononclick(i){     array.push("element" + + "clicked");      if(     (array.includes(element1clicked&&element2clicked&&element3clicked)) ||     (array.includes(element4clicked&&element5clicked&&element6clicked)) ||     (array.includes(element7clicked&&element8clicked&&element9clicked)))         {console.log("you won game!"}    }  } 

edited:

<script>      var i="";     var a="o";     var tablica1=[""];      (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>  

you may check strings , 1 element each check.

if (array.includes('element1clicked') &&     array.includes('element2clicked') &&     array.includes('element3clicked')) {     // ... } 

shorter code array#every

if (['element1clicked', 'element2clicked', 'element3clicked'].every(s => array.includes(s)) ||     ['element4clicked', 'element5clicked', 'element6clicked'].every(s => array.includes(s)) ||     ['element7clicked', 'element8clicked', 'element9clicked'].every(s => array.includes(s))) {     // ... } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -