function findLargestNumbers(arrayOfArrays) {
let k = new Array;
for(let i = 0 ; i < arrayOfArrays.length ; i++){
if(arrayOfArrays[i].length == 0){
continue ;
}
let ans = arrayOfArrays[i][0];
for(let j = 0 ; j < arrayOfArrays[i].length ; j++){
ans = Math.max(ans,arrayOfArrays[i][j]);
}
k[i] = ans;
}
return k;
}
What I was trying to do is get the max numbers things this thing is really bad what is even the word limit on this this must be enough information right you guys will get it what the hell is even happening
[[], [1, 2], []]
expected [2]
2
Answers
just keep another variable for k[i] then everything will be fixed.
Since you add items to the result array by index (
k[i] = ans;
) in case of an empty inner array you skip the index, leaving it empty (undefined)<1 empty item>
For me it’s totally OK since that indicates an empty inner array. If you want to avoid this, use
Also you could start looping the inner array starting from
1
since you’ve already written its first element intoans
:A couple of functional alternatives: