# Return Largest Numbers in Arrays - working for only one solution

The below solution only works for largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]])

``````function largestOfFour(arr) {
// You can do this!

var temp = [];

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

return temp;
}

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

It fails for the other numbers. I believe the reason for this is that i am using temp[i] instead of creating a new variable for the largest number and then using temp[i] to add the largest number. The below code works. My question is that: why can i not use temp[i] directly, why do i need to create a seprate variable largestNumber? why does it work for the first solution?

For example:

``````   for (var i = 0;i < arr.length;i++){
largestNumber = 0
for (var j = 0;j < arr[i].length;j++){
if (arr[i][j] > largestNumber){
largestNumber = arr[i][j];
}
}
temp[i] = largestNumber;
}
``````

When you post code, put three backticks (```) before and after it to preserve formatting.

Think about this line in your first solution: `if (arr[i][j] > temp[i])`
What is the value of `temp[i]` at this line? What does that logic evaluate to? Use console.log statements and look in the Javascript console of your browser (in the developer tools, usually F12) if you need to see more.

I’ve edited your post for readability, by wrapping your code within triple-backticks. You can do this yourself when posting code by hilighting the code and pressing the `</>` button at the top of the editor for pre-formatted text, or by including the code within triple backticks, like so:

`````
`````
For the function problem, InternetFriend has hit the nail on the head. You create your temp array: `var temp = [];`, but that just creates the array container. Possible solutions are to either give temp[i] a really low value (eg the lowest value available to javascript number types) at the start of the outer loop, or when checking `if arr[i][j] > temp[i]`, also check to see if `temp[i]` is undefined, and modify it accordingly.
well, `temp[i]` as first thing is undefined.
Try `console.log(3 > undefined; 3 < undefined)` and see what happens