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

146
Vistas
Variable's value changes for no reason

I am trying to make a simple tiktakto game with html, css, js. I am making a gamemode where you can square up against the computer. This code is supposed to check if a column should be blocked by the bot:

for(let i = 0; i <= 2; i++)
    {   
        for(j = 0; j <= 2; j++)
        {
            console.log("j = ", j)
             
            S += board[j][i];
        }  // <-- inside for loop j only goes from 0 to 2

        if(S === 2) // <-- inside of this if statemnt j is 3
        {   
            console.log("j = ", j) 
            if(prevent(i, j, "column")) return 1;
            else return 0;
        }
        S = 0;
    }

However somewhere after for loop the value of j randomly goes from 2 to 3 before the prevent() function can be executed, why?

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

0

This for loop:

for (j = 0; j <= 2; j++) {
  console.log("j = ", j)

  S += board[j][i];
}

Is equivalent to this while loop:

j = 0
while (j <= 2) {
  console.log("j = ", j)

  S += board[j][i];

  j++
}

So if j <= 2 is true the body loop body is executed and also the j++.

about 4 years ago · Juan Pablo Isaza Denunciar

0

The for loop will only stop once the condition evaluates to false. Here, the first time j <= 2 is false is when j has been incremented to 3.

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