Completed Code
Copy var mainArray = [[ 68 , 47 , 85 , 22 ] , [ 86 , 75 , 30 , 9 ] , [ 482 , 829 , 147 , 643 ] , [ 3906 , 2222 , 1337 , 4962 ]];
function largestMultiple (mainArray) {
return mainArray .map ( function (subArray) {
return Math . max .apply ( null , subArray);
});
}
console .log ( largestMultiple (mainArray));
Explanation
Remember the .map()
method? We use it to apply our function to each of the subArrays , the arrays within our mainArray .
Smallest
As we did previously, you can simply replace the .max()
with .min()
to get the smallest number from each of the four subArrays . I also changed largestMultiple to smallestMultiple so that the function makes sense.
Copy var mainArray = [[ 68 , 47 , 85 , 22 ] , [ 86 , 75 , 30 , 9 ] , [ 482 , 829 , 147 , 643 ] , [ 3906 , 2222 , 1337 , 4962 ]];
function smallestMultiple (mainArray) {
return mainArray .map ( function (subArray) {
return Math . min .apply ( null , subArray);
});
}
console .log ( smallestMultiple (mainArray));
Ultimate Large
If you wanted to get the one largest value between the four arrays, all you need is another Math.max.apply
.
Copy var mainArray = [[ 68 , 47 , 85 , 22 ] , [ 86 , 75 , 30 , 9 ] , [ 482 , 829 , 147 , 643 ] , [ 3906 , 2222 , 1337 , 4962 ]];
function largestMultiple (mainArray) {
return mainArray .map ( function (subArray) {
return Math . max .apply ( null , subArray);
});
}
console .log ( largestMultiple (mainArray));
console .log ( Math . max .apply ( null , largestMultiple (mainArray)));
Output
Copy [ 85 , 86 , 829 , 4962 ]
4962
Ultimate Smallest
Use the same code to get the one smallest value , but don't forget to replace .max()
with .min()
. There are a few occurrences of it.
Copy var mainArray = [[ 68 , 47 , 85 , 22 ] , [ 86 , 75 , 30 , 9 ] , [ 482 , 829 , 147 , 643 ] , [ 3906 , 2222 , 1337 , 4962 ]];
function smallestMultiple (mainArray) {
return mainArray .map ( function (subArray) {
return Math . min .apply ( null , subArray);
});
}
console .log ( smallestMultiple (mainArray));
console .log ( Math . min .apply ( null , smallestMultiple (mainArray)));
Output
Copy [ 22 , 9 , 147 , 1337 ]
9