JS-152-Objects-Arrays
  • Introduction
  • 04-Objects
    • OOP-Basics
      • Overview
      • Coding the Example
    • Prototypes
      • Object Prototypes
      • Prototypes Continued
    • Inheritance
      • Inheritance
    • Synopsis
      • Synopsis
    • Challenges
    • Solutions
      • Constructor Function
        • Person Constructor
        • Explanation
      • Inheritance Challenge
        • Code
        • Explanation
      • Family Challenge
        • Child
        • Pet
  • 05-Arrays
    • Array Basics
      • Array Overview
      • Creating an Array
      • Individual Elements and Length
      • Iterating Over Arrays
    • Array Methods
      • Methods Overview
      • .join() Method
      • .reverse() Method
      • .split() Method
      • .replace() Method
      • .splice() Method
      • .map() Method
      • .indexOf() Method
      • .filter() Method
      • .every() Method
    • Palindrome Algorithm
    • Array Challenges
    • Solutions
      • First and Last
        • First Element
        • Last Element
      • Most Frequent
      • Largest and Smallest
        • Single Array
        • Multiple Arrays
      • Missing Number
      • "Arrays" in the DOM
      • Sorting
        • Bubble Sort
        • Selection Sort
    • Resources
Powered by GitBook
On this page
  • Completed Code
  • Explanation
  • Smallest
  • Ultimate Large
  • Ultimate Smallest
  1. 05-Arrays
  2. Solutions
  3. Largest and Smallest

Multiple Arrays

Completed Code

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.

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.

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

[ 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.

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

[ 22, 9, 147, 1337 ]
9
PreviousSingle ArrayNextMissing Number

Last updated 7 years ago