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: