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?