# Basic Algorithm Return Largest Numbers in Arrays

Basic Algorithm Return Largest Numbers in Arrays
0

#1

I don’t know the reason why i get wrong with this code!
anyone can help me fix it and show me the errors here please ha!

``````function largestOfFour(arr) {
var newarr =[];
for(var i =0;i<4;i++){
var largest = 0;
for( var j =0; j < arr[i].length;j++){
if(largest < arr[j]){
largest = arr[j];
}
newarr.push(arr[j]);
}
}
return newarr;
}

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

#2

You’re not accessing the subarrays. You have to use the format array[i][j] to access the subarray.

#3

I believe it wants an array of values, one for each subarray.

#4

Instead of using arr[j], which is grabbing the whole sub array at index j, use arr[i][j] which is grabbing the value of index j in sub array i which is what you want.

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

if (largest < arr[i][j]) {
largest = arr[i][j];
}

newarr.push(arr[i][j]);

}

#5

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a 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.