Return Largest Numbers in Arrays
Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.
[Option 1] Set up: create a variable to return it to focus on our end result which is a sorted array.

Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i].

arr.sort(compareFunction);// note that (b - a) orders descending order (largest to smallest)function compareFunction(a, b) {
return b - a;
}
[Option 2] solving it using forEach() to iterate
Review of forEach function. Start simple. Example: for each that logs out each number in an array.

[1,2,3].forEach(function (num){
console.log(num)
}); // 1,2,3Now, combine .forEach()’s power with .sort().
var num = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]];function test (num) {
num.forEach(function(item) {
item.sort(function(a,b) {
return b-a;
});
});
return num;
}test(num); // output: [ [ 5, 4, 3, 1 ], [ 27, 26, 18, 13 ], [ 39, 37, 35, 32 ], [ 1001, 1000, 857, 1 ] ]
Use for loop to extract first index from each subarray.
// we have sub array inside of array
var num = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]];// output array with subarr sort b-a (high - low)
function test (num) {
// for forEach with sort to work, array needs to be 2-D array
// since .sort already sorts 1-D array we are enhancing it by adding
// .forEach iterates main array then .sort iterates subarray
num.forEach(function(item) {
item.sort(function(a,b) {
return b-a;
});
});
var resultSortedArr = [];
for(var i = 0; i < num.length; i++) {
resultSortedArr[i] = num[i][0];
}
return resultSortedArr;
}
// debugger;
test(num);
[Option 3] FCC solution breakdown:

Review on how to update array.

I want to know if I could get to the subArray index [0] and create a result array.

Now, finally add if statement to compare largest number.

// output: [5, 27, 39, 1001]




