Forked from anonymous/bonfire-return-largest-numbers-in-arrays.js
Created
December 4, 2015 17:16
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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