Skip to content

Instantly share code, notes, and snippets.

  • Save anonymous/b23b96fe770f4d5b73eb to your computer and use it in GitHub Desktop.
Save anonymous/b23b96fe770f4d5b73eb to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/terdia 's solution for Bonfire: Return Largest Numbers in Arrays
bonfire-return-largest-numbers-in-arrays?solution=function%20largestOfFour(arr)%20%7B%0A%20%20%2F%2F%20You%20can%20do%20this!%0A%20%20%0A%20%20%0A%20%20var%20result%20%3D%20%5B%5D%3B%0A%20%20var%20i%3B%0A%20%20var%20j%20%3D%200%3B%0A%20%20%0A%20%20%2F%2Floop%20through%20the%20main%20array%20element%20with%20i%0A%20%20for(i%20%3D%200%3B%20i%20%3C%20arr.length%3B%20i%2B%2B)%7B%0A%20%20%20%20var%20base_check%20%3D%200%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2Floop%20through%20the%20inner%20array%20element%20with%20j%0A%20%20%20%20for(j%20%3D%200%3B%20j%20%3C%20arr.length%3B%20j%2B%2B)%7B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%2F%2Fget%20the%20value%20of%20the%20current%20inner%20array%20element%20in%20the%20loop%0A%20%20%20%20%20%20%20%20var%20value_from_inner_array%20%3D%20arr%5Bi%5D%5Bj%5D%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%2F%2Fcheck%20if%20that%20is%20greater%20than%20base%20check%20value%20then%20assign%20the%20value%20to%20base%20check%0A%20%20%20%20%20%20%2F%2Frepeat%20the%20process%0A%20%20%20%20%20%20%20%20if(value_from_inner_array%20%3E%20base_check)%7B%0A%20%20%20%20%20%20%20%20%20%20base_check%20%3D%20value_from_inner_array%3B%0A%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20%2F%2Fadd%20the%20value%20to%20the%20result%20array%20%0A%20%20%20%20result.push(base_check)%3B%0A%20%20%20%20%0A%20%20%7D%0A%20%20%0A%20%20return%20result%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.js
// Bonfire: Return Largest Numbers in Arrays
// Author: @terdia
// Challenge: http://www.freecodecamp.com/challenges/bonfire-return-largest-numbers-in-arrays?solution=function%20largestOfFour(arr)%20%7B%0A%20%20%2F%2F%20You%20can%20do%20this!%0A%20%20%0A%20%20%0A%20%20var%20result%20%3D%20%5B%5D%3B%0A%20%20var%20i%3B%0A%20%20var%20j%20%3D%200%3B%0A%20%20%0A%20%20%2F%2Floop%20through%20the%20main%20array%20element%20with%20i%0A%20%20for(i%20%3D%200%3B%20i%20%3C%20arr.length%3B%20i%2B%2B)%7B%0A%20%20%20%20var%20base_check%20%3D%200%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2Floop%20through%20the%20inner%20array%20element%20with%20j%0A%20%20%20%20for(j%20%3D%200%3B%20j%20%3C%20arr.length%3B%20j%2B%2B)%7B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%2F%2Fget%20the%20value%20of%20the%20current%20inner%20array%20element%20in%20the%20loop%0A%20%20%20%20%20%20%20%20var%20value_from_inner_array%20%3D%20arr%5Bi%5D%5Bj%5D%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%2F%2Fcheck%20if%20that%20is%20greater%20than%20base%20check%20value%20then%20assign%20the%20value%20to%20base%20check%0A%20%20%20%20%20%20%2F%2Frepeat%20the%20process%0A%20%20%20%20%20%20%20%20if(value_from_inner_array%20%3E%20base_check)%7B%0A%20%20%20%20%20%20%20%20%20%20base_check%20%3D%20value_from_inner_array%3B%0A%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20%2F%2Fadd%20the%20value%20to%20the%20result%20array%20%0A%20%20%20%20result.push(base_check)%3B%0A%20%20%20%20%0A%20%20%7D%0A%20%20%0A%20%20return%20result%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) {
// You can do this!
var result = [];
var i;
var j = 0;
//loop through the main array element with i
for(i = 0; i < arr.length; i++){
var base_check = 0;
//loop through the inner array element with j
for(j = 0; j < arr.length; j++){
//get the value of the current inner array element in the loop
var value_from_inner_array = arr[i][j];
//check if that is greater than base check value then assign the value to base check
//repeat the process
if(value_from_inner_array > base_check){
base_check = value_from_inner_array;
}
}
//add the value to the result array
result.push(base_check);
}
return result;
}
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