Skip to content

Instantly share code, notes, and snippets.

@bflannery
Created December 20, 2016 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bflannery/49e4d7ae3d607ddc24c62dc720ff7369 to your computer and use it in GitHub Desktop.
Save bflannery/49e4d7ae3d607ddc24c62dc720ff7369 to your computer and use it in GitHub Desktop.
Return Largest Number In An Array
// Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain 4 sub-arrays.
function largestOfFour(arr) {
let 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;
}
console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));
console.log(largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]));
console.log(largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment