# Return Largest Numbers in Arrays - Type Error: Cannot read property '1' of undefined

Return Largest Numbers in Arrays - Type Error: Cannot read property '1' of undefined
0

#1

Tell us what’s happening:

I put this code together but was getting a few errors. Specifically this "Type Error: Cannot read property ‘1’ of undefined.

I took a glance at the answer on the hint page and understand how that code works. I’m just trying to understand why I’m getting this error and if this code would work despite being a messier solution.

Thanks.

``````function largestOfFour(arr) {
// You can do this!
var largest = 0;
var largest_array;
for (i = 0; i < arr.length; i++){

for (j = 0; j < arr[j].length; j++){

if (arr[i][j] > largest){

largest = arr[i][j];

if (j === (arr[j].length - 1) )
largest_array.push(largest);
largest = 0;
}
arr = largest_array;
}
return arr;

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

Your Browser User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36`.

#2

Your code has several logic errors, but the one causing the error you are currently getting is the following line:

``````arr = largest_array;
``````

At the end of first iteration of the outer for loop, you set assign arr the value undefined, because largest_array was declared at the top of your function but was never initialized to anything. JS automatically defaults an uninitialized variable as undefined. You received the error TypeError: Cannot read property ‘1’ of undefined because arr was undefined instead of an array, so arr[j].length results in an error, because you can not access undefined[1] (j was 1 after the start of the 2nd iteration).

Below I have indented your code to better reflect the blocks for your for loops and if statements. It was harder to see which parts were within each block with your previous indentation format.

``````function largestOfFour(arr) {
// You can do this!
var largest = 0;
var largest_array;
for (i = 0; i < arr.length; i++){
for (j = 0; j < arr[j].length; j++){
if (arr[i][j] > largest){
largest = arr[i][j];
if (j === (arr[j].length - 1) )
largest_array.push(largest);
largest = 0;
}
arr = largest_array;
}
return arr;
}
}
``````

#3

Thanks. I guess I overlooked that part. Made a few other changes and got it to output correctly.

function largestOfFour(arr) {

var largest = 0;
var largest_array = [];

``````for (i = 0; i < arr.length; i++){
for (j = 0; j < arr[i].length; j++){

if (arr[i][j] > largest){

largest = arr[i][j];

}

if (j === (arr[j].length - 1) ) {
largest_array.push(largest);
largest = 0;
}

}

}
``````

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

#4

Glad you got it figured out, but even though the FCC tests do not cover this situation, your solution would not return the correct answer if the largest of a subarray was a negative number. For example,

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

The above would return

[ 0, 27, 39, 1001 ]