# Should special case be inside or outside the for loop here?

For example, suppose I have 2 arrays:

``````let arr1=[5,2,1];
let arr2=["abcde","ab","a"];
``````

my work is simple : to check if length of strings in arr2 are larger than corresponding element with same index in arr1, so:

``````let arr1=[5,2,1];
let arr2=["abcde","ab","a"];
``````

is true,

``````let arr1=[5,3,1];
let arr2=["abcde","ab","a"];
``````

is false,

``````let arr1=[5,2,1,1];
let arr2=["abcde","ab","a"];
``````

is also false. I'm struggling where I should put the checking of array length:

Style 1: put it outside for loop:

``````function isAllLarger(arr1,arr2){
if(arr1.length>arr2.length){
return false;
}
for(let i=0;i<arr1.length;i++){
if(arr2[i].length<arr1[i]){
return false;
}
}
return true;
}
``````

Style 2 : put it inside for loop:

``````function isAllLarger(arr1,arr2){
for(let i=0;i<arr1.length;i++){
if(i>=arr2.length|| arr2[i].length<arr1[i]){
return false;
}
}
return true;
}
``````

Which one should I use?

• This is a matter of opinion. In my opinion, I'd always select the first option as the intent of the code is clearer: check length of the arrays, then check the lengths of the elements. But that is just opinion: there's no right answer to this. – David Arno Sep 21 '18 at 8:43
• If the specification is "if the array lengths are unequal the answer is always FALSE", then checking boundaries over and over within the loop would be both wasteful and less readable. I see no upside to Style 2. – Kilian Foth Sep 21 '18 at 10:28