skip to Main Content

I have the following code:

let j = 0;
    for(i = 0; i < board[0].length; i++)
    {
        j = i;
        check_row(board[i], i, tiles);
        i = j; 
    }

The board is an array containing 5 arrays of size 5:
[[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4]]
Tiles is a nodeArray of size 25 if it’s relevant.
The check_row(…) function currently just contains console.log("test")

If I remove the
i = j
then check_row will only execute once, but if I keep i = j, then the loop will work as normal and call check_row() the intended 5 times, once for each row. I do not understand why passing the iterator breaks the loop. If I pass j instead of i and remove i = j, then I run into the same problem.

Any help would be apppreciated!

2

Answers


  1. Have you declared i in somewhere else too, it might be the problem with not adding let at the start of the for loop?

    for(let i = 0; i < board[0].length; i++)
    {
        check_row(board[i], i, 1);
    }
    
    Login or Signup to reply.
  2. Code used to try to reproduce (assuming length is 5):

    let j = 0;
        for(i = 0; i < 5; i++)
        {
            j = i;
            console.log(i);
            i = j; 
        }
    

    Unable to reproduce removing i = j as said, even when missing the let for i = 0;. The problem must be elsewhere in your code.

    Setting j = i then i = j is useless as pointed out by @derpirscher.
    If you need to store i, only j = i is required.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search