Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save robertsheacole/d30661594ff3ab034cfc to your computer and use it in GitHub Desktop.
Save robertsheacole/d30661594ff3ab034cfc to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/robertsheacole 's solution for Bonfire: Return Largest Numbers in Arrays
// Bonfire: Return Largest Numbers in Arrays
// Author: @robertsheacole
// 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) {
//This array will hold our largest number at the end
var newArray = [];
//Loop through outer array (which contains 4 items - the number groups)
for(var i = 0; i < arr.length; i++){
//Use this var to store largest num in each array and reset to 0 each time
var largestNumber = 0;
//Loop through inner array (the individual nums)
for (var j = 0; j < arr.length; j++){
//Check for biggest num of inner array and assign to largestNumber. Once largest num is found it can't be reset for that array item.
if (largestNumber < arr[i][j]){
largestNumber = arr[i][j];
}
}
//Now we have the largest num of each inner array, but they need to be put in our empty array that we defined at the top called newArray using the .push() method.
newArray.push(largestNumber);
}
//Finally, we have to return our results!
return newArray;
}
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