# Nesting for loops has me buffaloed [SOLVED]

Hello,

Welcome to the new forum I guess. This is my first venture in the new format so bear with me.

I am working through the lessons in order in the FreeCodeCamp curriculum and I have come upon a lesson on nesting for loops that is giving me fits. I don’t want so much to be fed the answer but I need an explanation of what’s going on here.
So, Here’s my code:

``````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++){
console.log(arr[i][j]);
}
}

product = product * ?????;

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

// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);
``````

What I understand is that there is a function called multiplyAll which takes an arguement(arr) I understand the nested for loops I think. The argument (arr) actually takes more than one paramenter or argument: like this [i,j] So the first for loop finds the i’s and the second one finds the j’s and and the console.log statement is supposed to multiply i and j together and multiply those products. Or I’m wrong about that.
In the answer to the problem product needs to be equal to all the products multiplied together. But the product= statement has me buffaloed.
So if you could help with the answer and an explanation of what I’m missing I’d be grateful
All the best,
luciano991

I’ve not done this lesson, so I’m confused about what you’re trying to achieve with the line:

`product = product * ????;`

However, I might be able to help clarify the argument passed into “multiplyAll” which may in turn help you understand that product line.

So, “arr” in this instance is a Multidimensional Array, or Array of Arrays. The first for loop says, “Okay, we have an array with 3 elements, lets loop through those.” At this point, it doesn’t know or care that each element in the array is in fact another array. Now the second for loop grabs the element retrieved by the first for loop and says, “Okay, I know you’re an array, lets loop through each element.”

So what I think you want to happen here is put that product assignment line where your console log is currently.

`product *= arr[i][j];`

Hello @luciano991, I’m going to try to explain what it does. I think you are a bit confused.

``````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++){
console.log(arr[i][j]); // <--------- This line does nothing but log 1 then 2 then 3 until 7.
//What you need to do is multiply here instead of logging. As suggested:
product *= arr[i][j]; //The i gives us the i array in the main array. (ex: first one would be [1,2])
// And the j gives the elements inside the array[i] (ex: first one would be 1)
}
}

//product = product * ?????;  <------- Commenting this line

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

// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);
``````

If you need further examples feel free to tag me, I’ll be happy to help. (maybe I’ll modify my reply depending on how it renders.)

It’s a bit confusing but remember that `[ ]` means there is an array, so if you have `[ [ ],[ ],[ ] ]` that would be an array with 3 arrays inside. Hope this helps.

Cheers!
Daniel S.

2 Likes

Thank you Daniel. Your explanation was indeed helpful. And I made the mistake of putting product *= arr[i][j]; outside of the function. Otherwise it mostly makes sense. Baby steps.

All the best,

Luciano991

1 Like

Yes, exactly. The ??? was to indicate that I didn’t know what to put there. I also had placed the product *= arr[i][j]; outside of the function. Rookie mistake.

Thanks for your time and trouble.

Appreciate the explanation.

Luciano991

Excellent @luciano991, the important thing is that you grasp the idea. If it is clear to you, I think it will help all campers if you edited the subject of your post and added (SOLVED) at the end. If you are still having trouble grasping the concept, I would gladly make several examples for you.

Cheers,
Daniel S.

You need to use triple backticks to post code to the forum.
See this post for details.

Thanks. I knew triple ticks worked in the chat but I didn’t know they worked in the forum.

Mark

How did you get `var i` and `var j`? I don’t understand where these two variables are coming from.

Hey @pcabrera83,

They are made up variables, it’s just to store the Index of the loop. It’s common to use i since it’s shorter than index, and j since it’s the next letter alphabetically.

I’m guessing you are having trouble with loops, if you have any questions about it let me know. It’s crucial you understand how to loop in arrays and nested arrays in programming! So don’t hesitate to ask.

Cheers,
Daniel S.

1 Like

this is where i feel like having a conversation with someone in person would help. I live in a small town and the closest FreeCodeCamp group is about 2 hours away.

using “j” just blows my mind because “j” is not mentioned in the code itself but for some reason it is used and I don’t know why.

and where does `.length` even come into the code?

and how would one know that

`product *= (arr[i][j]);`

1.) `*=` what does this mean?
2.) what does `arr[i][j])` mean and how would one know to input this?

I mean if anything maybe I would put

`(arr[i]*[j])`

using “j” just blows my mind because “j” is not mentioned in the code itself but for some reason it is used and I don’t know why.

It’s not in the code? It looks like it is to me.

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

There is nothing magical about i or j or whatever. The standard for loop is:

``````for (var i = 0 ; i < 10 ; i++ )
``````

But i is arbitrary. It probably stands for “iterator”, but it could have been any variable name:

``````for (var elephantPajamas = 0 ; elephantPajamas < 10 ; elephantPajamas++ )
``````

But people usually use i by tradition if they need a vanilla iterator in a for loop. And if i is taken, they use j, then k, etc. But you can use something else if it makes it clearer.

1.) *= what does this mean?

`product *= (arr[i][j])` is the same as saying `product = product * (arr[i][j])`

If you write `a *= b*`, that is saying "multiply a times b and assign it back into a, but you can write `a = a * b` if you want. But you’re going to see that a lot so you’d better get used to it. I think that it’s also technically a little faster.

2.) what does arr[i][j]) mean and how would one know to input this?

arr is an array of arrays, as defined in the code, `multiplyAll([[1,2],[3,4],[5,6,7]]);`

For example, if i and j are such that you are looking at `arr`, then that would evaluate as 6, because you are looking for the 2 element in the top level array (`[5,6,7]`, remember that we start counting with 0) and within that you are looking for the 1 element (6, remember that we are counting from 0).

The code is just using `arr[i][j]` to step through all the possible cells.

If that is not clear, then I’d do some research on arrays, especially multidimensional (nested) arrays. Don’t get frustrated - we all have stumbling blocks. This is a little weird until you wrap your head around it and then it’s easy.

Look for some tutorials. Youtube might be good because it is so visual.

3 Likes

thank you for the excellent guidance and support! It means a lot to me.

I’m confused about the equation and how product * arr[i][j] yields the results that it does.

`````` multiplyAll( [ [2,3] ] ); //this = 6 but if all we are doing is multiplying by 1 then I don't get how we get 6.

multiplyAll ( [ [2,3] ,[2,5 ] ]); // this = 60. I understand it is 2*3 =6  then 6 * (2*5) = 60
``````

What I don’t understand is how we get that from 1 * arr[i][j].

at what point is it multiplying against itself and not by 1?

I think I understand now maybe.`var product` starts off as 1 then changes throughout each iteration?
during lets say [3 ,4] it is 13, now product is 3 then 34 =12 ?

And every time it loops through a j it gets reset back to one? still a little confused as to when it multiplies against its old self meaning ( [ [1,2] ] ) product becomes 2 at that point but then it gets reset back to 1 to calculate the next array [ 3,4 ] so when do we get 2*12?

It’s not product * arr[i][j]. It’s product *= arr[i][j].

The `*=` is a special operator. If you do:

``````num *= 3;
``````

That is the same as doing:

``````num = num * 3;
``````

So, in your example of `[[2, 3], [2, 5]]`, let’s step through it. Taking away the loops, we are basically doing this:

``````let product = 1;
product * = 2; // 1 * 2 = 2
product * = 3; // 2 * 3 = 6
product * = 2; // 6 * 2 = 12
product * = 5; // 12 * 5 = 60
``````

You can run the JavaScript and put in some `console.log` statements to confirm.

You can read about `*=` here. MDN is a great resource.