Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joePichardo/0d6b0928f01febeea874 to your computer and use it in GitHub Desktop.
Save joePichardo/0d6b0928f01febeea874 to your computer and use it in GitHub Desktop.
Return an array consisting of the largest number from each provided sub-array.
// Bonfire: Return Largest Numbers in Arrays
// Author: @joepichardo
// Challenge: http://www.freecodecamp.com/challenges/bonfire-return-largest-numbers-in-arrays
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function largestOfFour(arr) {
// You can do this!
//empty array to place new numbers in
var largestArray = [];
var largestNumber = 0;
/*
two for loops go through the two-dimensional array
compares numbers within each sub-array to find the largest number
that number is then placed into our array
largestNumber is equaled to 0 after the second for loop is
finished so the next comparison is ready to go
*/
for(var i = 0; i < arr.length ;i++){
for(var j = 0; j < arr[i].length; j++){
if(arr[i][j] > largestNumber){
largestNumber = arr[i][j];
largestArray[i] = largestNumber;
}
}
largestNumber = 0;
}
return largestArray;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment