# More Javascript Trouble

I don’t know if anyone will remember this or not, but in the exercise that deals with nesting for loops, this was the solution:

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 = arr[i][j];
}
}
// Only change code above this line
return product;
}

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

I could have (barely) figured this out on my own, except for the bold excerpt above…how did anyone know that that was needed in this solution? I feel so frustrated at having to always look at the hints and other people’s solutions just to get past some of these exercises. That’s not what I’m here for, just to get past them…where is everyone actually LEARNING this stuff? Because it’s not here!

Thanks for reading my rant…

I would still stop looking up solutions. When you get stuck, its best to ask questions before looking up the answer.

Even if you looked up the solution and understood it, you have to make sure you can use it afterwards - don’t look at it, feel frustrated and go on with the next exercise. Try to find the solutions for similar problems. For example for this one:
What if not every item of arr was an array like here? `arr = [[1,2], 3, [4], 5, [6,7]]`
What would you have to do different?

Or you take an array like this `arr = [[1,2, 0], 3, [4], 5, 0, [6,7]]` and each time the loop encounters a 0 you want to switch from multiplying to dividing or back.

A bit more difficult: You don’t know how deep the array is, how would you do it then? (try recursion)

If you have trouble with thinking of your own exercises, maybe look for a website with ready made challenges. Don’t continue too fast if you have no experience in writing algorithms, because then you are currently learning how to solve such a problem and how to implement it in javascript

And while it’s common to name the variables there i and j, I sometimes call them outerIndex and innerIndex, so I always directly know in which position each one has to be:

``````for (let outerIndex = 0; outerIndex < arr.length; outerIndex ++) {
for (let innerIndex = 0; innerIndex < arr[outerIndex].length; innerIndex ++) {
product *= arr[outerIndex][innerIndex];
}
}

for (let i = 0; i < arr.length; i++) {
let innerArray = arr[i];
for (let j = 0; j < innerArray.length; j++) {
product *= innerArray[j];
}
}
``````

At the end another thing you could try: rename the subarray so you can pretend there is no nested array, just a simple array you need to loop through