Return Largest Numbers in Arrays with a for loop

Return Largest Numbers in Arrays with a for loop
0

#1

First off, this code definitely works, but I know I need to use a for loop. I tried using a for loop and couldn’t quite get it. How would I convert this code to a for loop?

function largestOfFour(arr) {
  // You can do this!
  var newestArray = [];
  var x = arr[0];
  var y = arr[1];
  var z = arr[2];
  var aa = arr[3];
  var newArray = x.sort(function(a, b) {return b - a;});
  var newArray1 = y.sort(function(a, b) {return b - a;});
  var newArray2 = z.sort(function(a, b) {return b - a;});
  var newArray3 = aa.sort(function(a, b) {return b - a;});
  var newerArray1 = newestArray.push(newArray[0]) + newestArray.push(newArray1[0]) + 
  newestArray.push(newArray2[0]) + newestArray.push(newArray3[0]);

  return newestArray;

}

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

#2

In this previous thread, I gave an example of a nested loop: http://forum.freecodecamp.com/t/find-the-longest-word-in-a-string/11702/18?u=jacksonbates

var array = [[1,2,3], [4,5,6], [7,8,9]]          // Array of arrays
for (var i = 0; i < array.length; i++) {         // Outer loop
  for (var j = 0; j < array[i].length; j++)  {   // Inner loop
    console.log(array[i][j]);                    // what do you expect 
  }                                              // this to log?
}

Look at this example carefully first and see if you can modify it to do what you want. If you still get stuck, gimme a nudge and I’ll give some more hints :slight_smile: