# Really confused on Nesting For Loops

So I’m really stuck on this one. I think I got the hang of loops up till now, but adding nesting is throwing me totally. I know I need to do multiplication somewhere in the loop, but I’m not sure how.

I know this must work something like accessing nested arrays in previous lessons, but I can’t get my head around it.

Any help welcome.

``````function multiplyAll(arr) {
let product = 1;
// Only change code below this line
for (let i = 0; i < arr.length; i * i) {
for (let j = 0; j < arr[i].length; j * j) {
product = arr[i][j];
console.log(product);
}
}
// 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 (iPad; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/102.0 Mobile/15E148 Safari/605.1.15`

Challenge: Nesting For Loops

Let’s start from something simple - how do you multiply two numbers together and store it in a variable?

1 Like

I’d do it like this,

``````let multiply = 0;
multiply = 1 * 2;
``````

Ok, second question, how do you iterate over all elements of an array?

Using a for loop like in this lesson.

Can you show me please? Can you write an example in a post?

No because i thought I knew how to do it, I passed the lesson before but have forgotten and I literally solved it today but already forgot it. I am very embarrassed.

Hi Ella

It looks as though you’re still a bit confused about for loops.

Try this FCC article - it might help.
(Don’t just read it, though. Try the code in the examples for yourself.)

1 Like

Oh goodness thanks that looks really useful!

Hi I’ve looked at these and ran them in codepen (except the ones that were really advanced and I haven’t leaned about yet) but I still don’t understand how to apply that to this lesson. I managed to solve the Iterate Through an Array with a For Loop Lesson again.

But I still don’t know how to solve this lesson. I feel like I’m just trying to jam code in to make it work.

I don’t understand why I can’t make the multiplication work.

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

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

`i` and `j` are index values. You need to use them to get to the elements inside the arrays. Them multiply the elements (not the indexes).

Look at the example code on how to access the elements.

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

And maybe check this challenge as well

2 Likes

Thank you! I solved it! Just needed to look closer at the example.