Skip to content

Instantly share code, notes, and snippets.

  • Save anonymous/ec04f1629d3a03d913b3 to your computer and use it in GitHub Desktop.
Save anonymous/ec04f1629d3a03d913b3 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/v3rse 's solution for Bonfire: Return Largest Numbers in Arrays
// Bonfire: Return Largest Numbers in Arrays
// Author: @v3rse
// Challenge: http://www.freecodecamp.com/challenges/bonfire-return-largest-numbers-in-arrays?solution=function%20largestOfFour(arr)%20%7B%0A%20%20%2F%2Freturned%20array%0A%20%20var%20rArr%20%3D%20%5B%5D%3B%0A%20%20%2F%2Fnormal%20for%20loop%0A%20%20for(var%20i%3D0%3B%20i%3Carr.length%3B%20i%2B%2B)%7B%0A%20%20%20%20arr%5Bi%5D.sort(function(a%2Cb)%7B%0A%20%20%20%20%20%20return%20b-a%3B%0A%20%20%20%20%7D)%3B%0A%20%20%20%20%0A%20%20%20%20rArr.push(arr%5Bi%5D%5B0%5D)%3B%0A%20%20%7D%0A%20%20return%20rArr%3B%0A%7D%0A%0AlargestOfFour(%5B%5B4%2C%205%2C%201%2C%203%5D%2C%20%5B13%2C%2027%2C%2018%2C%2026%5D%2C%20%5B32%2C%2035%2C%2037%2C%2039%5D%2C%20%5B1000%2C%201001%2C%20857%2C%201%5D%5D)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function largestOfFour(arr) {
//returned array
var rArr = [];
//normal for loop
for(var i=0; i<arr.length; i++){
arr[i].sort(function(a,b){
return b-a;
});
rArr.push(arr[i][0]);
}
return rArr;
}
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