# Return Largest Numbers in Arrays problem

My function works with all the other sample inputs. However, when I type largestOfFour([[3, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]); I end up getting : [27, null, 39, 10001] as my output. Can someone help me as to why I am getting a null on the second index. I don’t have this problem with all the other outputs. Here is my function:

``````function largestOfFour(arr) {
// You can do this!
var nArr = [];
var largest = 0;
for (var i = 0; i < arr.length; i++) {
//var largest = arr[i][0];
for (var j = 0; j < arr[i].length; j++) {
if (largest < arr[i][j]) {
//console.log(arr[i][j]);
largest = arr[i][j];
nArr[i] =largest;
}
}
}
arr = nArr.slice(0);

return arr;
}
``````

Hello,

to post code on the forums you need to enclose the whole block between triple back ticks.

I think I know where the problem is (I’m not much further than you) but I’m not sure I can explain.

However I think if you take the time to explain what you think is happening at every line, you’ll see the problem. In the meantime I’ll try to see if I can find a way to explain - and somebody else might help

Maybe this can help:
look at my comments and follow the logic. What happens when it’s time for the number 1 to be passed in the IF statement?

``````function largestOfFour(arr) {

var nArr = [];
var largest = 0;
for (var i = 0; i < arr.length; i++) { // so you're incrementing until 4 max

for (var j = 0; j < arr[i].length; j++) { // again until 4 in the inner arrays
if (largest < arr[i][j]) { // if 0 is < 3 ...

largest = arr[i][j];   // 0 becomes 3.  (so what happens when it's 1?)
nArr[i] =largest;    // nArr[i] (so nArr[0], nArr[1], etc) is 3, then ...
}
}
}
arr = nArr.slice(0);

return arr;
}

largestOfFour([[3, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
``````
1 Like

I cleaned up your code. Thanks for pointing this out too @timotheap
You need to use triple backticks to post code to the forum.
See this post for details.

1 Like

@timotheap So where do you think is the problem? Why number 3?

You set Largest to be 0 at the beginning, then you assign a new value ( `arr[i][j]`) only if largest is smaller than `arr[i][j]`. So when compared against 3, Largest is smaller so it becomes 3. Then it becomes 27, the biggest number in the first inner array, good.

But when it’s time to pass the second inner array, Largest is now 27, and so bigger than every number in that array. So when it’s time to compare Largest to 4, 5, 1, or 4 (the second inner array), nothing happens. Nothing in that array is taken into account. Normally, you should be able to retrieve 5 (the largest in that array).

(Damn it I hope I’m right, I’d hate to mislead you).

Oh my god, am I saying something stupid again??? And if so, can a moderator delete my two f**** up explanations? And send flowers to the OP…

Damn it @IsaacAbrahamson you’re needed for some cleanup…

By looking at your comments in the code I wasn’t sure what was your thought process, but your explanation cleared it for me. You are correct.

1 Like

thank G** for that, I wasn’t going to be able to sleep… I should, oh I really should stop trying to tackle stuff like that…

Don’t worry about being wrong sometimes. Happens to all. Just admit you were wrong and move on.

And stop those ninja edits

2 Likes

Lol, so true! I said something stupid earlier today and just left it. Its not like someone is going to search my hundreds of posts to see my stupid ones, and if they do…

1 Like