# Return Largest Numbers in Arrays Only Runs 3/4 Arrays

The code is supposed to return the the largest number of each array, and it does so for the first three arrays of the set but not the final one, and I’m really confused as to why that is the case.

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

var myArray = [];

for (var i = 0; i < arr.length; i++) {

if(i > 0){
myArray.push(max);
}

var max = 0;
for (var j = 0; j < arr[i].length; j++) {
if(arr[i][j]> max) {
max = arr[i][j];
}
}
}
arr = myArray;
return arr;

}

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

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (`</>`) will also add backticks around text.

Note: Backticks are not single quotes.

You’re almost there. This:

``````if(i > 0){
myArray.push(max);
}
``````

I don’t understand this. I get that you don’t want to push the data if there is not data yet, but why not just do the pushing after you get the data? If you simply moved this push to the “correct” place, you could lose the`if` and it would work. As it is now, you never push the results of the last max check. Trace through the code and see.

Also this:

``````  arr = myArray;
return arr;
}
``````

is an unnecessary step. Why not just return myArray? Also it’s considered a bad idea to reassign parameters that have been passed to your function, something that linters will complain about.

I would also suggest that you be more disciplined in your formatting and indenting - it will make your code easier to read and understand. It’s just a habit you develop, but it will save you headaches in the long run, especially as your code gets more and more complicated.

Ok! Thank you very much. I was able to fix it, but it still didn’t pass the test as setting max to 0 was giving me issues with negative numbers. It works now though, much appreciated. I tried making my format a little more disciplined, but I’m not sure if I quite understood what you meant by that.

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

var myArray = [];

for (var i = 0; i < arr.length; i++) {
var max = arr[i][0];
for (var j = 0; j < arr[i].length; j++) {
if(arr[i][j]> max){
max = arr[i][j]}
}
myArray.push(max);
}
console.log(myArray)
return myArray;
}
``````

OK, but at least it’s working for positive numbers now.

I can think of two solutions for negative numbers:

1. Set max to the lowest possible number. What is the lowest possible number in JS.
2. A slightly more efficient way would be to set max to the first number. And then you could just skip over the first one in you `for` loop.

Yeah, sorry I should have clarified. I changed max from being equal to 0 to being equal to arr[i][0] in my second post so it does work for all numbers.

Oh, I see. In that case you can start your j loop at 1 - no need to check if that number is greater than itself.

Also, I added `[spoiler]` tags around your answer. We discourage showing the answer in case some people don’t want to see.