Nesting For Loops --> I'm confused about a comment made in the instructions to this challenge

There is a comment in this lesson that I want clarification on…

Note that for the inner loop, we are checking the .length of arr[i], since arr[i] is itself an array.

What I think js permits is that you could use either i or j in the nested for loop condition and would get the same result. What you can not do is use both i and j in the nested for loop condition (it would not execute correctly then). So my confusion about the comment in this lesson is they seem to be saying the the variable i must be used rather than clarifying that it isn’t which variable you use but whether you use both together or just one of them (in the condition).

Am I mistaken about this? Is there something I’m not seeing and the variable j can not be used even if it is simply replacing i in the nested for loop condition?

Here is an example using code to illustrate what I’m talking about…


// I want to make sure I understand what their comment is saying : https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/nesting-for-loops


const arr = [
  [1, 2], [3, 4], [5, 6]
];

for (let i = 0; i < arr.length; i++) {
  for (let j = 0; j < arr[i].length; j++) {
    console.log(arr[i][j]);
  }
}


// [quote] ...Note that for the inner loop, we are checking the .length of arr[i], since arr[i] is itself an array.

// What I think they are getting at is this..

// You can have..


const arr = [
  [1, 2], [3, 4], [5, 6]
];

for (let i = 0; i < arr.length; i++) {
  for (let j = 0; j < arr[j].length; j++) { // i replaced with j
    console.log(arr[i][j]);
  }
}


// ..because it will execute the same way as the original example (see line 10 where the variable i is use instead of j)

// But you can not have..

const arr = [
  [1, 2], [3, 4], [5, 6]
];


for (let i = 0; i < arr.length; i++) {
  for (let j = 0; j < arr[i][j].length; j++) { // both i and j being used
    console.log(arr[i][j]);
  }
}


// ..because this will not execute correctly

// Do I have that right? Does that seem to be what they are actually getting at?

Your code so far


function multiplyAll(arr) {
let product = 1;
// Only change code below this line

// 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 (X11; Ubuntu; Linux x86_64; rv:96.0) Gecko/20100101 Firefox/96.0

Challenge: Nesting For Loops

Link to the challenge:

That comment isn’t about the choice of variable name. That comment is just saying that the inside loop needs to run the length of the arr[i] and we get the length by using the .length property.

1 Like

Basically they are stating that by using the .length property on arr[i], you are getting the length of the array at the individual index positions of const arr. [i] accesses the values at those positions, so by using arr.length you get the length of const arr (3), and by using arr[i].length you get the length of the array at that specific index position (in this case 2). You still want to use i in the inner loop to iterate though the nested arrays, you just put the [] around it to access the nested array as opposed to the index position itself. As for the naming conventions of the variables in the for loops, i and j are just arbitrary, widely agreed upon names for for loop variables. it could just as easily be

for (let j = 0; j < arr.length; j++) {
  for (let i = 0; i < arr[j].length; i++) {
    console.log(arr[j][i]);
  }
}

or

for (let q = 0; q < arr.length; q++) {
  for (let p = 0; p < arr[q].length; p++) {
    console.log(arr[q][p]);
  }
}

As long as the inner loop arr[?].length refers back to the variable of the outer loop, it will work.

2 Likes

Thanks but my question is not related to variable naming but to variable choice. In this challenge there are two indexing variables one named i and the other named j. What I’m trying to address is the connection between length and variable choice as it relates to inner / outer loop. What I’m saying is they are choosing to use the index variable that comes from the outer loop in the inner loop (namely i) and they seem to be making a point of it - as though it has to be that way. Now I don’t care what the variable name is the point is that its the outer loop’s variable that is being used (in one place) in the inner loop as opposed to using the inner loop’s variable throughout that loop’s parameters. Now I think it doesn’t make a difference to the actual execution of the code (maybe there is some convention or best practice) but the loop should execute the same if we write it…

for (let i = 0; i < arr.length; i++) {
  for (let j = 0; j < arr[j].length; j++) { // Using the SAME variable as the inner loop's variable in that condition statement
    console.log(arr[i][j]);
  }
}

All this makes me question exactly what the message is in the statement…

Note that for the inner loop, we are checking the .length of arr[i] , since arr[i] is itself an array

The reality of the matter is this :

const arr = [
  [1, 2], [3, 4], [5, 6]
];

^ That array right there has not one array dimension but two (there are arrays with their own, individual length inside the outer one). That means there is not just one length but more than one length. In this case all of the inner arrays have the same length but it does not have to be that way. Not only that but there is a difference between the length of the outer and inner arrays (in this example). The outer array has a length of 3 elements while the inner arrays each have a length of 2 (different lengths). So if what they are saying is that i has to be used to measure the length of the inner arrays then how does that work? Do you see what I’m saying now?

================================================

I’m really sorry. I see it now. It’s in the use of bracket notation that the answer is seen…


const arr = [
  [1, 2], [3, 4], [5, 6]
];

for (let i = 0; i < arr.length; i++) { // There are not square brackets here --> meaning you are simply measure the length of the outer array
  for (let j = 0; j < arr[i].length; j++) { // There are square brackets here --> meaning that you are selecting the element of the outer array
    console.log(arr[i][j]);
  }
}

In one case you are measuring in the other case you are selecting (and then measuring what has been selected).

Is the right?

==========================

:rage:

fCC will not let me post another reply saying that as a new user I’v reached my limit. All I was trying to say was thanks and I get it. Here’s what I would have replied to @Loggith if I had been allowed to do so…

Yes, thank you so much for bearing with me. I don’t know why I didn’t see the difference between arr and arr[i]

1 Like

Lets add some comments

for (let i = 0; i < arr.length; i++) {
  // This is a loop over the contents of arr
  // so it can only go the length of arr
}

The outer loop is a loop over the array arr. That length doesn’t seem to be confusing to you.

for (let i = 0; i < arr.length; i++) {
  // This is a loop over the contents of arr
  // so it can only go the length of arr
  console.log("i: " + i);
  console.log(arr[i]);
}

The inner loop is over each sub-array that is stored inside of arr. We want to go through that sub-array, so we want the length of that sub-array:

for (let i = 0; i < arr.length; i++) {
  // This is a loop over the contents of arr
  // so it can only go the length of arr
  console.log("i: " + i);
  console.log(arr[i]);
  for (let j = 0; j < arr[i].length; i++) {
    // This is a loop over the array arr[i]
    // so it can only go the length of arr[i]
    console.log("j: " + j);
    console.log(arr[i][j]);
  }
}

Using arr[j] wouldn’t make any sense in this context.

2 Likes

If we have:

const arr = [
  [1, 2], [3, 4], [5, 6]
];

for (let i = 0; i < arr.length; i++) {
  for (let j = 0; j < arr[i].length; j++) {
    console.log(arr[i][j]);
  }
}

This line:
for (let i = 0; i < arr.length; i++) {
iterates though the outer array (const arr).

This line:
for (let j = 0; j < arr[i].length; j++) {
iterates though each of the nested arrays one at a time as the loop is iterated. The reason the variable i needs to be used in the inner loop to refer to the outer loop, is because we are using the variable i to reach each position in const arr and then [i] in the inner loop refers to the value of that variable’s current position.

I think you’ve got it now!

2 Likes