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:

Return Largest Numbers in Arrays Solution

JavaScript

1

for(vari=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 arr 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.

Next we have:

Return Largest Numbers in Arrays Solution

JavaScript

1

for(varj=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.

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:

JSHint Metrics for Return largest Numbers in Arrays algorithm

JavaScript

1

2

3

4

There isonly one functioninthisfile.

It takes one argument.

Thisfunctioncontains6statements.

Cyclomatic complexity number forthisfunctionis4.

Solution:

Return Largest Numbers in Arrays Solution

JavaScript

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

functionlargestOfFour(arr){

"use strict";

varmultiArr=[0,0,0,0];

for(vari=0;i<arr.length;i++){

for(varj=0;j<arr[i].length;j++){

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

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

}

}

}

returnmultiArr;

}

largestOfFour([

[

4,5,1,3

],

[

13,27,18,26

],

[

32,35,37,39

],

[1000,1001,857,1]

]);

Solution in ES6:

Return Largest Numbers in Arrays Solution in ES6

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

functionlargestOfFour(arr){

constmultiArr=[0,0,0,0];

for(leti=0;i<arr.length;i++){

for(letj=0;j<arr[i].length;j++){

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

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

}

}

}

returnmultiArr;

}

largestOfFour([

[

4,5,1,3

],

[

13,27,18,26

],

[

32,35,37,39

],

[1000,1001,857,1]

]);

Share this:

Like this:

Related

Maria Campbell

Founder, Inter-Global Media Network, Inc., where I focus on Front End Development. as well as a digital photographer, videographer, blogger, broadcaster, and graphic designer. Genesis Framework user. I got my start 8+ years ago teaching myself HTML/CSS and Wordpress Development because I couldn't find anyone who could create what I wanted. I have been working with open source software ever since. I am an open source software evangelist, and a "Women in Technology" evangelist. A creative professional myself, I find Web Development to be both practical and creative. I love to cook, travel, read mysteries, love everything French, the beach at sunrise, my three cats, and hope to revisit the city of San Francisco one day.

Reader Interactions

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.