Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda