window.addEventListener('click', function (e) { if (e.target.className == "cell") { this.openCell(e.target) } }.bind(this)) this.arr = []; this.flag ; } openCell(domElement) { for (let i = 0; i < this.field.length; i++) { if (domElement == this.field[i].cellElement()&& domElement !== this.flag) { this.arr.push(this.field[i]); this.flag = domElement; for (let k = 0; k < this.arr.length; k++) { this.arr[k].removeId(); if (this.arr.length > 2) { if (this.arr[0].cellElement().getAttribute("data") == this.arr[1].cellElement().getAttribute("data")) { this.arr[0].empti(); this.arr[1].empti(); this.flag = '' } else { this.arr[0].addId() this.arr[1].addId() this.flag = '' } this.arr.length = 0; } } } } } 

The first click - I push the element into the array (I open it there), the second click - I push the second element into the array (I open it), the third click - clears the array, but does not add a new element to it. enter image description here

This state is after 2 clicks, on the third - the pictures are closed, but a new one does not open. Due to what an extra click.

    1 answer 1

    And what you wanted - you clear the array this.arr.length = 0; At the third click, first clean up and THEN add an item

    • I agree with you, there is an idea how to implement it? - Ruslan SSS
    • this.arr.push (this.field [i]); do this line after cleaning the array - Doigrales