Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save IAmAnubhavSaini/e6d47fa3979fae5006ac to your computer and use it in GitHub Desktop.
Save IAmAnubhavSaini/e6d47fa3979fae5006ac to your computer and use it in GitHub Desktop.
function largestOfFour(arr) {
myArr = [];
arr.forEach(function(a){ myArr.push(a.sort(function(a, b){ return a-b;}).pop()); });
return myArr;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
@IAmAnubhavSaini
Copy link
Author

I can try to make it better via FiF.

function largestOfFour(arr) {
  myArr = [];
  function pushHighestFromArray(a){
    myArr.push(a.sort(function(a, b){ return a-b;}).pop());
  }
  arr.forEach(pushHighestFromArray);
  return myArr;
}

@IAmAnubhavSaini
Copy link
Author

I think I can still make it better.

function highestInArray(a){
    return (a.sort(function(a, b){ return a-b;}))[a.length-1];
}

function largestOfFour(arr) {
    myArr = [];
    arr.forEach(function(a){ myArr.push(highestInArray(a)); });
    return myArr;
}

I think there is a bit of good in this code.

@IAmAnubhavSaini
Copy link
Author

I am trying to wrap my head around arr.map(Function.apply.bind(Math.max, null));
@abhisekp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment