Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

89
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!