0

I have a problem in my code:

var row = ["1","2","3","4","5"];
var column = ["1","2","3","4","5"];

var arrayLength = row.length;
var arrayLength2 = column.length;
for (var i = 0; i < arrayLength; i++) {
    for (var e = 0; e < arrayLength2; e++) {
        var samples = document.querySelectorAll('[data-row-id="'+row[i]+'"][data-column-id="'+column[e]+'"]');

        for(var i = 0; i < samples.length; i++) {
            var sample = samples[i];

            sample.setAttribute('data-sample-id', row);
            console.log("Colore cambiato");
        }
    }
}

When i run it, the cycle lasts infinitely and the console.log is called up a lots of times

Where is the error? Thanks!

1
  • Gotta love those dynamic arrays Commented Jun 30, 2017 at 16:14

2 Answers 2

3

The problem is that your inner-most loop uses the same i looping variable as your outer most loop and it's constantly changing i so that the outer loop never finishes.

Change the variable on your inner loop to a different identifier that you aren't already using in the same scope.

Sign up to request clarification or add additional context in comments.

Comments

1

Youre using same loop i twice nested so it runs infinitely coz it always resets i in inner loop

use something else instead like k

   for(var k = 0; k < samples.length; k++) {
        var sample = samples[k];

        sample.setAttribute('data-sample-id', row);
        console.log("Colore cambiato");
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.