Does anyone know what would cause my for loop to continue after the conditions are met? I would post the entire file, but it is quite large. Here are the loops:
for (var j = 0; j < depKeyArr.length; j++) {
var directory = angular.copy(dependencies[depKeyArr[j]]),
keyArr = angular.copy(Object.keys(directory));
for (var i = 0; i < keyArr.length; i++) {
var next = i + 1;
if (keyArr[i] && !keyArr[i].includes('Params')) {
if (keyArr[next] && keyArr[next].includes('Params')) {
directory[keyArr[next]] = directory[keyArr[next]].split(', ') !== [''] ? directory[keyArr[next]].split(', ') : undefined
directory[keyArr[next]].push(directory[keyArr[i]])
}
var paramArr = directory[keyArr[next]] ? directory[keyArr[next]] : directory[keyArr[i]]
switch (depKeyArr[j]) {
case 'controller':
angular.module('app').controller(keyArr[i], paramArr)
break
case 'directive':
angular.module('app').directive(keyArr[i], paramArr)
break
case 'factory':
angular.module('app').factory(keyArr[i], paramArr)
break
case 'filter':
angular.module('app').filter(keyArr[i], paramArr)
break
case 'service':
angular.module('app').service(keyArr[i], paramArr)
break
}
}
}
}
So depKeyArr.length
in this particular instance equals 5, and keyArr.length
equals 42. Both the for loops continue to execute when those conditions are met, in spite of the fact that I would expect them to stop at 4 and 41. Any help you can offer so I can figure out why would be great.
Snippet updated above!
Also - you all mentioned i & j changing, but when I'm looking at it in the debugger, they are unchanged. I am looking at it right now - j = 5 and depKeyArr.length
is 5. Similarly, i = 42 and keyArr.length
is 42...and yet I'm inside both loops, specifically at the last case in the switch statement. It fails when keyArr[i] is undefined.
In response to the answer provided by OBDM, I added angular.copy()
to prevent updating the array from inside the loop. It is still stepping into the loop when the boolean condition is met. What is even more perplexing is the fact that even the if
condition doesn't keep it from executing the switch. It should fail to enter the following if
:
if (!keyArr[i].includes('Params'))
because by then keyArr[i]
is undefined.
Latest update -
It seems that I have successfully broken out of the inner loop with the latest changes, but it still continues to the switch statement and breaks on keyArr[i]
even though that switch is within both loops and an if
conditional which evaluates to false.