Can anyone help me understand why my for loop is continuing even after the conditions are met? Unfortunately I can't share the entire file due to its size, but here is a snippet of 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
}
}
}
}
In this scenario, depKeyArr.length
is 5, and keyArr.length
is 42. Despite these values, both loops continue beyond the expected limits. Any insights on why this might be happening would be greatly appreciated.
Snippet above has been updated!
I have noticed mentions of i and j changing, however, in my debugging session they remain unchanged. Currently, j is at 5 with depKeyArr.length
also at 5. Similarly, i is at 42 while keyArr.length
stands at 42...yet the loops proceed further than expected, specifically stopping at the last case in the switch statement. This failure occurs when keyArr[i] is undefined.
In response to OBDM's suggestion, I incorporated angular.copy()
to avoid modifying the array within the loop. However, the loop still executes when the condition should prevent it. What's more puzzling is that even the if
condition doesn't halt the execution of the switch statement. It should not enter the following if
:
if (!keyArr[i].includes('Params'))
as by that point, keyArr[i]
is undefined.
Latest update -
Although I successfully managed to exit the inner loop with recent adjustments, the code continues to the switch statement and throws an error on keyArr[i]
despite being within both loops and an if
statement that evaluates to false.