Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

91
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda