I am stuck on this challenge. I’m not sure what I am doing wrong. It passes the first three tests but the fourth it fails. Can anyone tell me what I am missing? Thanks
Your code so far
function largestOfFour(arr) {
var largest = [0,0,0,0];
for(var i = 0; i < arr.length; i++) {
for(var j = 0; j < arr[i].length; j++) {
if(arr[i][j] > largest[i]) {
largest[i] = arr[i][j];
}
}
}
// You can do this!
return largest;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.
Ah. took me a minute, but think through the logic: you’re initializing your largest array with zeroes, assuming your values will all be larger than zero.
What is happening in the last case that could break your assumptions?
the first part makes total sense to me - the zeroes are the problem because of the negative numbers that come up - they are less than zero and so that’s going to cause an error. But I’m not sure how to solve this. Taking the zeroes out and making it an empty array passes only the first test. There’s a bit of logic here I’ve missed that I’m just not seeing, and the fact I’m not seeing it makes me wonder if I need to go back and do some remedial studying (and if so, at which point do I begin again? eeks)
thanks for your fast reply, and any additional insight you can provide
would you mind showing me what that would look like? I tried a couple of different ways inside my outer loop and I’m just not getting it. thank you so much for your help on this.
And you can actually avoid the comparison between the first element of the array and itself, they are the same because you are building largest like that - to make it a tiny bit more efficient you just need to change a single thing in there
And rather than give you the answer outright, consider this – you’re already setting largest[i] = arr[i][j] inside the inner loop, right? So, just after the OUTER loop starts, do what @ILM just said.