# Return largest number array challenge!

Here is my code!

``````function largestOfFour(arr) {
// You can do this!
var retArray=[];
var max=0;

for(i=0;i<arr.length;i++)
{
for(p=0;p<arr[i].length;p++)
{
if(arr[i][p]>max)
{
max=arr[i][p];

retArray=arr[i];

}

}
}

return retArray;
}

largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [2, 35, 3, 3], [10, 11, 7, 1]]);
``````

It is showing correct answer in the console,but not passing the testcases.

While I am looking at your code, I would appreciate it if you could edit your post to make it easier to read.

Highlight everything that is code, then look at the little toolbar above the place where you write your message. Click the button that looks like `</>`, then save.

EDIT: In addition, it would be very helpful if you could comment your code so I can see what you are thinking at each step.

EDIT 2: It seems to me like you are misunderstanding the question. You donâ€™t need to return the sub-array with the biggest element in it. You need to return an array containing the biggest element of EACH of the arrays.

You need to use triple backticks to post code to the forum.
See this post for details.

What the problem ask for is to return the array that is make up from the largest number of itâ€™s own array. Your retArray must have 4 number that are selected from each sub arrays. This mean your retArray must equal to [27, 5, 35, 11] for your given test case.

In order to do that, you must first search the largest number within sub array and push it to your retArray.

What you are doing right now is searching the largest number from all given number and returning the whole array that contains the largest number. Your retNumber is holding [2, 35, 3, 3].

P.S >> Iâ€™m a non-english speaker and Iâ€™m sorry if I make mistake and I canâ€™t say cleanly. Have fun and good luck!

Thanks will keep this in mind before posting a question next time:)

Thanks:), sorry i misunderstood the question!

1 Like

``````function largestOfFour(arr) {
var array = arr;
var finalArray = [];
for (var i = 0; i < array.length; i++) {
``````
``````    var biggestCurrentNumber = 0;
for (var n = 0; n < array[i].length; n++) {

if (array[i][n] > biggestCurrentNumber) {

biggestCurrentNumber = array[i][n];
}
}
finalArray.push(biggestCurrentNumber);

}
return finalArray;
}
``````

largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [2, 35, 3, 3], [10, 11, 7, 1]]);

2 Likes

You are correct. You need to reset the biggest number for each new array.

2 Likes

This is exactly what I needed-- my largest number var was up top and not resetting. As soon as I moved it downâ€¦ Thanks for posting this

``````function biggestNum(arr) {
var max = arr[0];
arr.forEach(function (el) {
if (el > max) max = el;
});
return max;
}

function biggestCollection(arrs) {
return arrs.map(function (arr) {
return biggestNum(arr);
});
}
``````

`function largestOfFour(arr) { for(var i=0; i<arr.length; i++){ arr[i] = arr[i].reduce(function(large,current){ return current > large ? current : large; },-1); } return arr; }`

and another one
`function largestOfFour(arr) { return arr.map(x=>x.reduce(function(large,current){ return current > large ? current : large; },-1)); }`[details=Summary]This text will be hidden[/details]

You could just sort and then take the latest. ItÂ´s sad because the sort in JS by default is alphabetical so you need to pass a sortFunction to the method but yeahâ€¦ In general terms this solution is the best in other languages since normally the sort under the hood uses a really good algorithm

function largestOfFour(arr){
return arr.sort((a,b) => a - b )[arr.length -1];
}

1 Like

I used sort exactly in this way and ignored the comment about having â€śonly four elements in each array for simplicityâ€ť by taking the last element after sorted. It seems to be passing any test and even matches the expected output under the instructions, but only passes for returning an array.

``````var newArr = [];
var n; var x;

function largestOfFour(arr) {
for (n = 0; n < arr.length; n++) {
var rep = []; var i;
rep = arr[n].sort(function(a,b) { return a - b;});
i = rep.pop();
newArr.push(i);

}

return newArr;
}

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

I was about to sort the final array as well after noticing I was getting the largest of each array, but realized that doesnâ€™t seem necessary in the solution, so does anyone know whatâ€™s wrong here?

Sort should be super quick in this case @jdmerinor - if the array is all integers, most JS engines will switch to using a very fast C integer sort afaik

Also

``````function largestOfFour(arr){
const largestNumInArr = (nums) => Math.max(...nums);
return arr.map(largestNumInArr);
}
``````
3 Likes

Hello everyone, Iâ€™m getting stuck with this one. Iâ€™ve read in google other ways to solve this, and looked at the codes here but I wrote my own which passes all the test cases when I log it to the console in Sublime but donâ€™t understand why it wonâ€™t pass on the website. Is it because Iâ€™m not using comparison operators?

Hereâ€™s my code:
// new array to be returned
var newArray = [];

function largestOfFour(arr) {
// for loop to iterate through the array
for (i = 0; i < arr.length; i++) {
// using max and apply functions to extract largest number and storing them
var largest = Math.max.apply(Math, arr[i]);
// pushing into an array to be returned
newArray.push(largest);
}
return newArray
}

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

Thank you. Dumb mistake on my part.

Hi,

My code is not passing however it shows the correct answer.

``````var array = [];

function largestOfFour(arr) {
for (var i = 0; i < arr.length; i++){
var lgN = arr[i].sort(
function(a, b) {
return a - b;
}).slice(-1);

array.push(lgN[0]);
}
return array;
}
``````

Thank you

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the new value. To fix this, make sure your function doesnâ€™t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

``````var myGlobal = [1];
function returnGlobal(arg) {
myGlobal.push(arg);
return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
var myLocal = [1];
myLocal.push(arg);
return myLocal;
} // reliable - always returns an array of length 2
``````