Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

121
Visualizações
Something like TIC TAC TOE in JavaScript

I was thinking to do a simple tic tac toe game, but instead of adding X and 0, I was thinking when a square is clicked, that square should become red, then next click on another square to make it blue.

All was ok, until the squares turn only into red when I click on them and on next click they don't became blue. I tried different ways, without success. Finally I wrote the code like so, to be very clear that after .red class was added on the div that was clicked, the value of current player should change, and then because it changed, on next click to add the .blue class on clicked div. But it's becoming red, not blue. What should I do, so on next click the div should turn into blue? Thanks

let score=0
let blue ='blue',red = 'red'
let currentPlayer = red



const cells = [] // to keep all cell to iterate over them


  for(let i=0;i<9;i++){
    const divCell = document.createElement('div')
    divCell.setAttribute('class', 'cell')
    divCell.setAttribute('id', i)
    gridWrapper.appendChild(divCell)
    cells.push(divCell)

}


cells.forEach( cell => {
    cell.addEventListener('click', (e)=> {
       let id = e.currentTarget.id
        if(!cells.id && currentPlayer === red){
            cells[id] = currentPlayer
            e.target.classList.add('red') 
            currentPlayer = blue
            console.log(currentPlayer);
         } 
        if(!cells.id && currentPlayer === blue){
            cells[id] = currentPlayer
            e.target.classList.add('blue') 
            currentPlayer = red
            console.log(currentPlayer);

        }
       
    })
})```
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I think you have a logical error with the cells array check see in pic suppose to be cells[id] and not cells.id

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório

0

On each click, both if blocks are being invoked. Because changes made in the first if block are satisfying the second condition. Make the second if an else if:

if (!cells.id && currentPlayer === red) {
  //...
} else if (!cells.id && currentPlayer === blue) {
  //...
}

That way each click only invokes one player's turn rather than both.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda