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

262
Vistas
When trying to play again after playing again, one of the squares don't color

https://jsfiddle.net/h0avkn8p/is my entire code

When you play the game twice, you will see that the correct square position from the last game stays white, does anyone know where the problem is? I think it is coming from this part of my code.

function genSquareColors(){
    if(arr.length > 0){
        arr.splice(0, arr.length);
    }
    for(let i = 0; i < 5; i++){
        arr.push(genWrong()); // generates five incorrect squares
    }
    arr.push(answer);
    scramble(arr);
    console.log(arr);
    return arr;
   
}

function setColor(){
    for(let i = 0; i < squares.length; i++){
        squares[i].style.backgroundColor = arr[i];  
    }

} 

function colorSquares(){
    for(let i = 0; i < squares.length; i++){
        squares[i].addEventListener("click", function(){
            var clicked = this.style.backgroundColor;
            if(clicked === answer){
                alert("You won!");
                reset();
                this.style.backgroundColor = setColor();
             } else {
                this.style.backgroundColor = "#FFFFFF";
                tries--;
            }
        });

    }
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I see that here:

function colorSquares(){
    for(let i = 0; i < squares.length; i++){
        squares[i].addEventListener("click", function(){

You're attaching a new event listener to each square every time the game runs. So, starting on the second game, a click will produce two results for each listener. On the third game, there will be three results - etc. So a click can result in a square both "winning", and being colored white.

Either save a reference to each listener in a persistent variable, then call removeEventListener with each one of them - or use .onclick instead, so that only the latest assignment is kept.

function colorSquares(){
    for(let i = 0; i < squares.length; i++){
        squares[i].onclick = function(){
            var clicked = this.style.backgroundColor;
            if(clicked === answer){
                alert("You won!");
                reset();
                this.style.backgroundColor = setColor();
             } else {
                this.style.backgroundColor = "#FFFFFF";
                tries--;
            }
        };

    }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Whats going on here is your event listener is being created on the same items over and over and never being cleaned up. If you console.log out the in the event IE.

    for(let i = 0; i < squares.length; i++){
        squares[i].addEventListener("click", function(){
            var clicked = this.style.backgroundColor;
            console.log("oh no!")
            if(clicked === answer){
                alert("You won!");
                reset();
                this.style.backgroundColor = setColor();
             } else {
                this.style.backgroundColor = "#FFFFFF";
                tries--;
            }
        });

    }
}

You will notice that the click event is being called many times.

The fix would be to store the event once its applied and then if the event is set, reset it. IE.

https://jsfiddle.net/CynderRnAsh/aukbnsp1/8/

Or apply the event only once ie. https://jsfiddle.net/CynderRnAsh/aukbnsp1/11/

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