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

90
Visualizações
How do alert something only after certain buttons are clicked?

I have 9 different divs in an array each attached to a forEach and event listener. I'm wondering how I would console.log a string only after three of the buttons are clicked. If this helps you understand my question better, I'm making tic tac toe, and need an alert when someone wins from clicking in the intended areas. Thanks!

HTML

    <div class="game-container">
        <div class="cell" id="0"></div>
        <div class="cell" id="1"></div>
        <div class="cell" id="2"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
    </div>

const cell = document.querySelectorAll('.cell');
player1 = 'X'
player2 = 'O'
let currentPlayer = player1

cell.forEach(element => {
    element.addEventListener('click', function (e) {
        if (currentPlayer === player1) {
            element.style.backgroundColor='red'
            currentPlayer = player2;
        } else {
            currentPlayer = player1;
            element.style.backgroundColor='Blue'
        }
    })
})
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Create on object that will store game state in a 3*3 array and will be notified on each click. After that he change his state (add one clicked cell) and will check if somebody win. If yes - log

about 4 years ago · Juan Pablo Isaza Relatório

0

You should create a state for winning cases in the game and check if user-selected cells are matching anything in a winning case.. maybe this repo is useful for you. https://github.com/AhmedMalekX/tic-tac-toe-game

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