Help w/ returning largest numbers in an arrays

Tell us what’s happening:

I am so close to getting this thing down, but when it comes to negative numbers (passing the last test), I am so dead. If I set the array numbers to negative numbers just to pass the test, then I know I’m cheating. If I empty the array and push the array values in, the final result is always an empty array (totalArr[i].push(arr[i][j]);).

Please helpfully help.


Your code so far

function largestOfFour(arr) {
// You can do this!
var totalArr = [0,0,0,0];
for(var i = 0; i < arr.length; i++){
for(var j = 0; j < arr[i].length; j++){
  if(arr[i][j] > totalArr[i]){

return totalArr;

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

Why start with an array of zeros? Is there a way for you to pick the initial array based on your inputs?

1 Like

So starting with a blank array [] would be the right choice?

To find them smallest value of the pieces, I’d compare each one to the other(s) as they are handed out to me.

I’ve been reading your explanation over and over for like five minutes, but I’m still totally stuck and am wondering how this relates to the algorithm.

Right, so when you have the first piece of paper, what’s your best guess at what’s the biggest value?

  1. The first one is given to me.
  2. I then receive the second piece of paper.
  3. I look at them both and pick the one with the smaller numeric value as the smallest one.
  4. The third piece of paper is given to me.
  5. I look at that one. If its value is greater than that of the previously selected one, then the previous smallest keeps its title of the smallest. If not, then the newest piece of paper is selected as the smallest one.
  6. Finally, the last piece of paper is handed to me. I will compare it with the smallest value I have now. If the piece of paper most recently handed to me has a lesser value than that of the last one, IT will be selected as the smallest one and the algorithm ends. If the paper most recently handed to me has a value greater than that of the last one, then the previous one keeps its title of the lesser of all the others. The algorithm ends.

My best (and only) guess at which has the greatest value would be that first piece of paper.

Maybe by making the array equal to the smallest values of each and then finding the largest ones?

You told me to find the smallest value of each piece of paper. If you meant to find the largest, then I would go over the same algorithm again and instead of finding the one with the smallest value, try to find the largest.

How I would know I’ve found the largest number is when I’ve iterated through them all (after going through each step I explained).

brother @ConnerOw1115 I solved that same problem by using the Math.max/min methods, I won’t tell you the answer but a little hint: to pass an array to one of these methods do


good luck

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.