**The Challenge:**

1 2 3 |
Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays. Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i]. |

**The solution should return:**

1 2 3 4 5 6 7 8 |
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return an array. largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return [27,5,39,1001]. largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000]. |

**Some helpful links:**

**Starter Code:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
function largestOfFour(arr) { // You can do this! return arr; } largestOfFour([ [ 4, 5, 1, 3 ], [ 13, 27, 18, 26 ], [ 32, 35, 37, 39 ], [1000, 1001, 857, 1] ]); |

**What my solution should achieve:**

- Should return an array consisting of the largest number from each provided sub-array. Each provided array will have 4 sub-arrays.

There are similarities between the solution to this algorithm and the solution to Find The Longest Word in a String. Check out my post on Find The Longest Word in a String to find out why. Can you spot the similarities?

Because I was provided with a fixed number of sub-arrays from which to extract the largest number in each, I declared a new variable called ** multiArr** and assigned it an array with 4 0s. These 0s are simply placeholder numbers. Each 0 holds a placeholder value against which to compare each element in each sub-array.

We have to analyze what is going on in the nested for loops and how they relate to the new ** multiArr** and the provided multi array

*.*

**arr**First we have:

1 |
for (var i = 0; i < arr.length; i++) {} |

This is the outer for loop. It refers to the provided multi array ** arr**. i is the counter for the for loop, and it will iterate over the length of

**as long as it is < arr.length. After the first iteration, i increments by 1, and the for loop starts its second iteration and so on.**

*arr*Next we have:

1 |
for (var j = 0; j < arr[i].length; j++) {} |

This is the inner for loop. For every iteration of the outer for loop, the inner for loop iterates through the length of the sub-array of ** arr**, represented by arr[i].length.

**Now let’s create a written articulation of the nested loops and how they relate to multiArr and arr (I did the same thing for Find The Longest Word in a String):**

**1st iteration of the outer for loop:**

(counter) i = 0

arr.length = 4

is i < arr.length? Yes.

i++ doesn’t happen until after the first iteration.

**1st iteration of the inner for loop:**

(counter) j = 0;

arr[i].length is arr[0].length;

arr[0].length = 4

is j < arr[0].length? Yes.

Then the condition that helps determine which element in arr[0] is the largest:

if(multiArr[i] < arr[i][j]) {

multiArr[i] = arr[i][j];

multiArr[0] = 0; arr[0][0] = 4

Is multiArr[0] < arr[0][0]? Yes. So the condition is true. -> mutliArr[0] is reassigned the value of arr[0][0].

multiArr[0] = 4.

The inner loop continues, because j is still < arr[0].length.

**2nd iteration of the inner for loop:**

counter j = 1;

multiArr[0] = 4

arr[0].length = 4

Is j < arr[0].length? Yes.

Then the condition that helps determine which element in arr[0] is the largest:

if(multiArr[i] < arr[i][j]) {

multiArr[i] = arr[i][j];

multiArr[0] = 4; arr[0][1] = 5

Is multiArr[0] < arr[0][1]? Yes. So the condition is true. -> multiArr[0] is reassigned the value of arr[0][1].

multiArr[0] = 5

The inner loop continues, because j is still < arr[0].length.

**3rd iteration of the inner for loop:**

counter j = 2

multiArr[0] = 5

arr[0].length = 4

is j < arr[0].length? Yes.

Then the condition that helps determine which element in arr[0] is the largest:

if(multiArr[i] < arr[i][j]) {

multiArr[i] = arr[i][j];

multiArr[0] = 5; arr[0][2] = 1

Is multiArr[0] < arr[0][2]? No. multiArr[0] > arr[0][2], so multiArr[0] remains the same, and there is no reassignment.

The inner for loop continues, however, because j < arr[0].length.

**4th iteration of the inner for loop:**

counter j = 3

multiArr[0] = 5

arr[0].length = 4

is j < arr[0].length? Yes.

Then the condition that helps determine which element in arr[0] is the largest:

if(multiArr[i] < arr[i][j]) {

multiArr[i] = arr[i][j];

multiArr[0] = 5; arr[0][3] = 3

Is multiArr[0] < arr[0][3]? No. multiArr[0] > arr[0][3], so multiArr[0] remains the same, and there is no reassignment.

j is now = arr[0].length -> 4 = 4, and iteration of arr[0] stops. The program goes back to the outer for loop and continues there.

**2nd iteration of the outer for loop:**

(counter) i = 1

arr.length = 4

is i < arr.length? Yes.

**1st iteration of the inner for loop:**

(counter) j = 0;

arr[i].length is arr[1].length;

arr[1].length = 4

is j < arr[1].length? Yes.

Then the condition that helps determine which element in arr[1] is the largest:

if(multiArr[i] < arr[i][j]) {

multiArr[i] = arr[i][j];

multiArr[1] = 0; arr[1][0] = 13

Is multiArr[1] < arr[1][0]? Yes. So the condition is true. -> mutliArr[1] is reassigned the value of arr[1][0].

multiArr[1] = 13.

The inner loop continues, because j is still < arr[1].length.

**2nd iteration of the inner for loop:**

(counter) j = 1;

arr[1].length = 4

is j < arr[1].length? Yes.

Then the condition that helps determine which element in arr[1] is the largest:

if(multiArr[i] < arr[i][j]) {

multiArr[i] = arr[i][j];

multiArr[1] = 13; arr[1][1] = 27

Is multiArr[1] < arr[1][1]? Yes. So the condition is true. -> mutliArr[1] is reassigned the value of arr[1][1].

multiArr[1] = 27.

The inner loop continues, because j is still < arr[1].length.

**3rd iteration of the inner for loop:**

(counter) j = 2;

arr[1].length = 4

is j < arr[1].length? Yes.

Then the condition that helps determine which element in arr[1] is the largest:

if(multiArr[i] < arr[i][j]) {

multiArr[i] = arr[i][j];

multiArr[1] = 27; arr[1][2] = 18

Is multiArr[1] < arr[1][2]? No. multiArr[1] > arr[1][2], so multiArr[1] remains the same, and there is no reassignment.

The inner loop continues, because j is still < arr[1].length.

**4th iteration of the inner for loop:**

(counter) j = 3;

arr[1].length = 4

is j < arr[1].length? Yes.

Then the condition that helps determine which element in arr[1] is the largest:

if(multiArr[i] < arr[i][j]) {

multiArr[i] = arr[i][j];

multiArr[1] = 27; arr[1][3] = 26

Is multiArr[1] < arr[1][3]? No. multiArr[1] > arr[1][3], so multiArr[1] remains the same, and there is no reassignment.

j is now = arr[1].length -> 4 = 4, and iteration of arr[1] stops. The program goes back to the outer for loop and continues there.

**3rd iteration of the outer for loop:**

(counter) i = 2

arr.length = 4

is i < arr.length? Yes.

**1st iteration of the inner for loop:**

(counter) j = 0;

arr[i].length is arr[2].length;

arr[2].length = 4

is j < arr[2].length? Yes.

Then the condition that helps determine which element in arr[2] is the largest:

if(multiArr[i] < arr[i][j]) {

multiArr[i] = arr[i][j];

multiArr[2] = 0; arr[2][0] = 32

Is multiArr[2] < arr[2][0]? Yes. So the condition is true. -> mutliArr[2] is reassigned the value of arr[2][0].

multiArr[2] = 32.

The inner loop continues, because j is still < arr[2].length.

**2nd iteration of the inner for loop:**

(counter) j = 1;

arr[i].length is arr[2].length;

arr[2].length = 4

is j < arr[2].length? Yes.

Then the condition that helps determine which element in arr[2] is the largest:

if(multiArr[i] < arr[i][j]) {

multiArr[i] = arr[i][j];

multiArr[2] = 32; arr[2][1] = 35

Is multiArr[2] < arr[2][1]? Yes. So the condition is true. -> mutliArr[2] is reassigned the value of arr[2][1].

multiArr[2] = 35.

The inner loop continues, because j is still < arr[2].length.

**3rd iteration of the inner for loop:**

(counter) j = 2;

arr[i].length is arr[2].length;

arr[2].length = 4

is j < arr[2].length? Yes.

Then the condition that helps determine which element in arr[2] is the largest:

if(multiArr[i] < arr[i][j]) {

multiArr[i] = arr[i][j];

multiArr[2] = 35; arr[2][2] = 37

Is multiArr[2] < arr[2][2]? Yes. So the condition is true. -> mutliArr[2] is reassigned the value of arr[2][2].

multiArr[2] = 37.

The inner loop continues, because j is still < arr[2].length.

**4th iteration of the inner for loop:**

(counter) j = 3;

arr[i].length is arr[2].length;

arr[2].length = 4

is j < arr[2].length? Yes.

Then the condition that helps determine which element in arr[2] is the largest:

if(multiArr[i] < arr[i][j]) {

multiArr[i] = arr[i][j];

multiArr[2] = 37; arr[2][3] = 39

Is multiArr[2] < arr[2][3]? Yes. So the condition is true. -> mutliArr[2] is reassigned the value of arr[2][3].

multiArr[2] = 39.

j is now = arr[2].length -> 4 = 4, and iteration of arr[2] stops. The program goes back to the outer for loop and continues there.

**4th iteration of the outer for loop:**

(counter) i = 3

arr.length = 4

is i < arr.length? Yes.

**1st iteration of the inner for loop:**

(counter) j = 0;

arr[i].length is arr[3].length;

arr[3].length = 4

is j < arr[3].length? Yes.

Then the condition that helps determine which element in arr[3] is the largest:

if(multiArr[i] < arr[i][j]) {

multiArr[i] = arr[i][j];

multiArr[3] = 0; arr[3][0] = 1000

Is multiArr[3] < arr[3][0]? Yes. So the condition is true. -> mutliArr[3] is reassigned the value of arr[3][0].

multiArr[3] = 1000.

The inner loop continues, because j is still < arr[3].length.

**2nd iteration of the inner for loop:**

(counter) j = 1;

arr[i].length is arr[3].length;

arr[3].length = 4

is j < arr[3].length? Yes.

Then the condition that helps determine which element in arr[3] is the largest:

if(multiArr[i] < arr[i][j]) {

multiArr[i] = arr[i][j];

multiArr[3] = 1000; arr[3][1] = 1001

Is multiArr[3] < arr[3][1]? Yes. So the condition is true. -> mutliArr[3] is reassigned the value of arr[3][1].

multiArr[3] = 1001.

The inner loop continues, because j is still < arr[3].length.

**3rd iteration of the inner for loop:**

(counter) j = 2;

arr[i].length is arr[3].length;

arr[3].length = 4

is j < arr[3].length? Yes.

Then the condition that helps determine which element in arr[3] is the largest:

if(multiArr[i] < arr[i][j]) {

multiArr[i] = arr[i][j];

multiArr[3] = 1001; arr[3][2] = 857

Is multiArr[3] < arr[3][2]? No. multiArr[3] > arr[3][2], so multiArr[3] remains the same, and there is no reassignment.

The inner loop continues, because j is still < arr[3].length.

**4th iteration of the inner for loop:**

(counter) j = 3;

arr[i].length is arr[3].length;

arr[3].length = 4

is j < arr[3].length? Yes.

Then the condition that helps determine which element in arr[3] is the largest:

if(multiArr[i] < arr[i][j]) {

multiArr[i] = arr[i][j];

multiArr[3] = 1001; arr[3][3] = 1

Is multiArr[3] < arr[3][3]? No. multiArr[3] > arr[3][3], so multiArr[3] remains the same, and there is no reassignment.

j is now = arr[3].length -> 4 = 4, and iteration of arr[3] stops. i is now = arr.length as well, and iteration of arr stops. The program leaves the nested for loops and executes the next statement outside of the nested for loops block.

The program returns multiArr right after the outer for loop’s closing curly bracket and right before function largestOfFour()’s closing curly bracket.

multiArr = [5, 27, 39, 1001].

**JSHint Metrics:**

1 2 3 4 |
There is only one function in this file. It takes one argument. This function contains 6 statements. Cyclomatic complexity number for this function is 4. |

**Solution:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
function largestOfFour(arr) { "use strict"; var multiArr = [0, 0, 0, 0]; for (var i = 0; i < arr.length; i++) { for (var j = 0; j < arr[i].length; j++) { if (multiArr[i] < arr[i][j]) { multiArr[i] = arr[i][j]; } } } return multiArr; } largestOfFour([ [ 4, 5, 1, 3 ], [ 13, 27, 18, 26 ], [ 32, 35, 37, 39 ], [1000, 1001, 857, 1] ]); |

**Solution in ES6:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
function largestOfFour(arr) { const multiArr = [0, 0, 0, 0]; for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr[i].length; j++) { if (multiArr[i] < arr[i][j]) { multiArr[i] = arr[i][j]; } } } return multiArr; } largestOfFour([ [ 4, 5, 1, 3 ], [ 13, 27, 18, 26 ], [ 32, 35, 37, 39 ], [1000, 1001, 857, 1] ]); |

Comment Policy:Your words are your own, so be nice and helpful if you can. Please, only use your real name and limit the amount of links submitted in your comment. We accept clean XHTML in comments, but don't overdo it please. Comments are moderated, so spammy or malicious links will be removed.