After solving the problem of the previous lesson, you should get the following code:
let cells = document.querySelectorAll('#field td');
start(cells);
function start(cells) {
for (let cell of cells) {
cell.addEventListener('click', function() {
this.textContent = 'X';
});
}
}
Let's now make alternation of crosses and zeros. To do this, I propose to introduce a counter of moves:
function start(cells) {
let i = 0; // the counter initial value
for (let cell of cells) {
cell.addEventListener('click', function() {
this.textContent = 'X';
i++; // increases the counter value
});
}
}
Having such a counter, we can easily implement the alternation of a cross and a zero: it is obvious that the X mark will appear at even counter values, and the O mark at odd ones.
Implement the described alternation of a cross and a zero.