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

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

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 Report

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 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!