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

146
Views
Why cant i change the value of something in an array

When i console.log the winConditions array it shows all zeroes when its supposed to have some 1's in it aswell.

I'm changing the value of a, b and c at the click event listener but idk why they are not changing

Someone please help me

    let a = 0,
        b = 0,
        c = 0,
        d = 0,
        e = 0,
        f = 0,
        g = 0,
        h = 0,
        i = 0;

    const test = document.querySelector(".gameboard");
    test.addEventListener("click", () => {
        a = 1;
        b = 1;
        c = 1;
        gameBoard.applyWinConditions;
        console.log(gameBoard.winConditions)
        gameBoard.gameFlow.checkScore();
    });



    const gameBoard = (() => {
        let gameBoard = {};
        let applyWinConditions = () => {  
            gameBoard.winConditions = [[a, b, c], [a, d, g], [a, e, i], [b ,e, h], [c, e, g], [c, f, i], [d, e, f], [g, h, i]]; 
            return gameBoard.winConditions;
        }
        const gameFlow = (function() {
            
            const _isEveryElementOne = (arr) => {
                return arr.every(el => el === 1) 
            }
            const _announceWinner = () => console.log("game ends");
        
            return {
                checkScore: () => {
                    console.log(gameBoard.winConditions);
                    if (gameBoard.winConditions.some(cond => _isEveryElementOne(cond))) return _announceWinner()
                    console.log("game continues")
                },
                
            };
        })();
        
        return {
            gameFlow,
            applyWinConditions: applyWinConditions(),
            winConditions: gameBoard.winConditions,  
        }
    })();

    gameBoard.applyWinConditions;
    console.log(gameBoard.applyWinConditions)
<button class="gameboard">Click me!</button>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I refactored your code as a class because I had trouble reading it. Note: I did not code all your methods. (e.g. checkscore and gameflow). This seems to work:

class Gameboard{
  winConditions = [];
  announceWinner = "Game Over";
  
  constructor(){
    this.winConditions = [[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]];
  }

  applyWinConditions(m){
   this.winConditions = [
      [m[0],m[1],m[2]],
      [m[0],m[4],m[5]],
      [m[0],m[4],m[8]],
      [m[2],m[4],m[6]],
      [m[2],m[4],m[5]],
      [m[2],m[5],m[7]],
      [m[3],m[3],m[4]],
      [m[6],m[7],m[8]]
   ]; 
   alert(this.winConditions); 
  }
  
  getWinConditions(){
    return this.WinConditions;
  }
  
  gameflow(){    
  }
  
  checkScore(){
  }
}// class

const gameBoard = new Gameboard();
const test = document.querySelector(".gameboard");

test.addEventListener("click", () => {
  gameBoard.applyWinConditions([1,1,1,0,0,0,0,0,0]);
});

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!