Return Largest Numbers in Arrays---------

Hey guys, first post! I’m going over this section again as I feel like I need to. Anyways I’m trying to figure this out on my own - sort of lol.

Can anyone point out what I’m doing wrong?

function largestOfFour(arr) {
var largestNumber = [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] > largestNumber[i]) {
largestNumber[i] = arr[i][j];
}
}
}
return largestNumber;
}

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

Welcome to FCC forums!

this is a whole lot of code for a simple sort and print situation. i suggest arr.forEach(subarray.sort(b - a)) and push subarray[0] to your largestNumber array. it can done in one line.

This challenge has some of the tests that have negative numbers as biggest number - so you would get a 0 there, because that is bigger than any negative number7
can you figure out what you could put there instead of 0?


@oristar2018 : everything can be done in one line… that doesn’t mean that people that are learning shouldn’t learn how to use the fundamentals

i don’t think urging him to simplify his code right now will do him any disservice. I wish someone had told me early on in my progression to think simple (ie Array.methods) and avoid double for loops if i m not doing complex traversals. plus usings methods always make the code more readable and the logic clearer; it’s a design pattern.

  • added var results = “”;
    -var largest number now has a value of arr[i][0]; ----- this is the step that still confuses me, why the 0? is it to return the the 0 index of the next array?
    -results[i] = largest number
    -return results
function largestOfFour(arr) {
  var results = [];
  for (var i = 0; i < arr.length; i++) {
    var largestNumber = arr[i][0];
    for (var j = 1; j < arr[i].length; j++) {
      if (arr[i][j] > largestNumber) {
        largestNumber = arr[i][j];
      }
    }

    results[i] = largestNumber;
  }

  return results;
}

------ im not too worried about simplifying my code rn, im more focused on being able to break the code down and understand each step

Yes. So now you are basing your comparison wholly on the values you retrieve from the passed array argument rather than the constant 0.

Might I suggest getting into the habit of using function tracers ALOT. using console.log() statements everywhere you are confused about logic or the state of an object/variable can really shine a light on what is happening compared to you what you are thinking.

For example…

function largestOfFour(arr) {
  var results = [];
  console.log("results = " + results);
  for (var i = 0; i < arr.length; i++) {
    var largestNumber = arr[i][0];
    console.log("iteration arr[" + i + "]. largestNumber = " + largestNumber);
    for (var j = 1; j < arr[i].length; j++) {
      console.log("iteration arr[" + i + "][" + j + "]");
      console.log("is " + arr[i][j] + " > " + largestNumber + "?");
      if (arr[i][j] > largestNumber) {
        console.log("Yes. Set largestNumber to " + arr[i][j]);
        largestNumber = arr[i][j];
        console.log("largestNumber is now " + largestNumber);
      } else { console.log("No."); }
    }
    console.log("the largest number in array " + arr[i] + " is " + largestNumber);
    results[i] = largestNumber;
    console.log("adding " + largestNumber + " to results. (" + results + ")");
  }

  return results;
}

copy and paste this into the challenge (note: you will need the input statement for the passed array to get any meaningful output).

this is an extreme example but is like a shining light that can guide you out of the forest of confusion.