I have two nested for loops; basically what I'm trying to do is inserting some rows inside an array that contains lines of text;

I look for the placeholder 

```
'// <<<PERMISSIONS TREE>>>'

```

and insert some lines just after it;
before doing this, I check if some lines are already there after the above placeholder and before

```
'// <<<END PERMISSIONS TREE>>>'

```

In this case I need first to delete these rows and then insert the new ones

```
for(i = 0; i < lines.length; i++) {
    if (lines[i].indexOf('// <<<PERMISSIONS TREE>>>') >= 0) {

        i++;
        var j = i;
        console.log(lines[j]);      //here lines[j] is defined and printed on screen correctly
        console.log(lines[j].indexOf('// <<<END PERMISSIONS TREE>>>'));    //this also works

        //start delete loop - the following line doesn't work
        while ((lines[j].indexOf('// <<<END PERMISSIONS TREE>>>') < 0) && (j < lines.length)) {
            lines.splice(j, 1);
            j++;
        }

        lines.splice(i , 0, result);  // insert result in the correct place, this works
        break;
    }
}

```

In the line where I start the while loop, I get the error:

```
Cannot call method 'indexOf' of undefined

```

but the two lines above (the logs) works; I just can't understand why...

This code runs in nodejs, don't know if this does matter;