Created
December 1, 2015 06:43
-
-
Save SerhiiLihus/70b5e779e85937ca6c46 to your computer and use it in GitHub Desktop.
Return Largest Number in sub 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: @serhiilihus | |
// http://www.freecodecamp.com/challenges/bonfire-return-largest-numbers-in-arrays#?solution=function%20largestOfFour(arr)%20%7B%0A%20%20var%20newArr%20%3D%20%5B%5D%3B%0A%20%20var%20max%3B%0A%20%20for%20(%20var%20i%20%3D%200%20%3B%20i%20%3C%20arr.length%20%3B%20i%2B%2B%20)%7B%0A%20%20%20%20max%20%3D%200%3B%0A%20%20%20%20for%20(%20var%20j%20%3D%200%20%3B%20j%20%3C%20arr.length%20%3B%20j%2B%2B)%0A%20%20%20%20%20%20if%20(arr%5Bi%5D%5Bj%5D%20%3E%20max)%20max%20%3D%20arr%5Bi%5D%5Bj%5D%3B%0A%20%20%20%20newArr.push(max)%3B%0A%20%20%7D%0A%20%20return%20newArr%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) { | |
var newArr = []; | |
var max; | |
for ( var i = 0 ; i < arr.length ; i++ ){ | |
max = 0; | |
for ( var j = 0 ; j < arr.length ; j++) | |
if (arr[i][j] > max) max = arr[i][j]; | |
newArr.push(max); | |
} | |
return newArr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment