How is the third value in array processed with just i and j?

Tell us what’s happening:
how does 7 get multiplied with just i and j? doesnt there need to be a k? multiplyAll has a third value in its third array. why does it work with just i and j?

Your code so far


function multiplyAll(arr) {
var product = 1;
// Only change code below this line
for (var i=0; i < arr.length; i++) {
  for (var j=0; j < arr[i].length; j++) {
    product *= arr[i][j];
  }
}

// Only change code above this line
return product;
}

multiplyAll([[1,2],[3,4],[5,6,7]]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0.

Challenge: Nesting For Loops

Link to the challenge:

1 Like

This code works because the array is a 2d array (2 dimensional array). It’s fine that 3rd element (which is an array) of the arr array has 3 values but it’s 2d array so that’s why it works.

2 Likes

for example [[1,2],[3,4],[5,6,7]]:

i :
index 0 : [1,2]
index 1 : [3,4]
index 2: [5,6,7]

j:
for [1,2] ,
index 0 : 1
index 1: 2

for[3,4],
index 0: 3
index 1: 4

etc…

so arr[i][j] would be 1,2,3,4,5,6,7, and multiply all together;

2 Likes

The short answer is because the i and j are used as the index number to access items in the arrays. The ‘i’ variable being used to acces each item in the outer array and ‘j’ being used to acces the items each of the inner arrays. These two variables ‘i’ and ‘j’ and how high their number will go are determined by the length of the array that you are currently working with.

If you review how to access a value in an array this is done by writing the name of the array followed by square brackets inside which you put a number which indicates which ‘index’ you want out of the array, with the first value being at the index of 0.

For example:

arr = [5,2,4]
arr[0]  # Returns 5
arr[1] # Returns 2
arr[2] # Returns 4
arr[3] # would result in an error as there is not a 4th item in the array.

If we want to access an item in a multi-dimensional array like we need to in this function, see below for an example of how we would do so.

arr = [[1,2],[3,4],[5,6,7]]

To access the 1 we would type arr[0] [0]. The first [0] gets us [1,2] and second [0] returns to us the 1. 

Reading your post it seems you have a undersanding of this. So let’s review what determines the value of i and j and when the numbers are changed.

The hard part to wrap your head around is how this for loop within a for loop works and how this correlates to accessing items in the arrays.

The first for loop will loop a total of 3 times due to arr.length being equal to 3. There is 3 sets of of arrays [1,2] , [3,4], and [5,6,7] and with ‘i’ increasing one each time the loop completes and will stop the loop once it hits the number 3 as it is not less than length of the array at that point. This is what makes sure we don’t try to access arr[3] which doesn’t exist.

Now for the second for loop. We need to access each item of each inner array. We use j for this. We need this number to increase by one while ‘i’ stays the same number. So once again to ensure we don’t try to access an index that doesn’t exist when we are looping through each inner array we get the length of each of the inner arrays. So the first time around i will equal 0, so arr[0].length = 2. So j will first equal 0, then 1, but not 2 as that will not be less than the length of arr[0].

So what end up with is the following things happening. I bolded the number that is being accessed each time during the loops.

First time around the first for loop
The i variable the first go around equals 0 so we are working with the first inner array within the entire array which equals [1,2].

The second for loop only goes twice since the length (amount of stuff) of the first inner loop is 2 ([1,2] and j increases by 1 each time around and stop looping once it is not less than length of [1,2] aka 2. So the results is below

product = 1

product *= arr[0][0] # which equals 1 [[1,2],[3,4],[5,6,7]]
product now equals 1 as 1 * 1 = 1
product *= arr[0][1] # which equals 2 [[1,2],[3,4],[5,6,7]]
product now equals 2 as 1 * 2 = 2

Second time around the first for loop
We have now completed one time through the first for loop so ‘i’ now increases by 1 and now equals 1 instead of 0. So we are now working with arr[1] which is equal to [3,4].

j is now starting from the beginning again as it completed all its loops ealier and it starts at 0 once again. This time it will loop 2 times again since just like the last time the length of arr[i] or arr[1] is 2 ([3,4]). So the below happens.

product *= arr[1][0] # which equals 3 [[1,2],[3,4],[5,6,7]]
product now equals 6 as 2 * 3 = 6
product *= arr[1][1] # which equals 4 [[1,2],[3,4],[5,6,7]]
product now equals 24 as 6 * 4 = 24

Third time around the first loop and last time
Now for the last time around and where you confusion is. The ‘i’ variable is now equal to 2. So we are now working with arr[2] which is equal to [5,6,7]. Once again the second for loops start from the beginning so j starts out by being equal to 0. This time j will go from 0 to 2 as the length of arr[i] or arr[2] is 3 ([5,6,7])

product *= arr[2][0] # which equals 5 [[1,2],[3,4],[5,6,7]]
product now equals 120 as 24 * 5 = 120
product *= arr[2][1] # which equals 6 [[1,2],[3,4],[5,6,7]]
product now equals 720 as 120 * 6 = 720
product *= arr[2][2] # which equals 7 [[1,2],[3,4],[5,6,7]]
product now equals 5040 as 720 * 7 = 5040

I hope this helps your understanding on how nested for loops let you work with multi-dimensional arrays. I also hope I didn’t typo somewhere.

5 Likes

I followed that completely. Thank you very much for the more thorough explanation. I find myself scratching my head throughout the javascript lessons, asking myself, “how”, or “why” does this work. Am i being a pain by asking these types of questions on here? Is there any hard copy material anyone would recommend to use to go along with these lessons for more informative explanations?