Nested Loops - I don't even know where to start!

Tell us what’s happening:
OMG this is embarrassing - mostly why I don’t post my solution, lol. I don’t even know where to start this one. This is after reading similar posts and short of reading the actual answer. Please be gentle, but is there any way to point me in the right direction without giving the answer, lol.

  **Your code so far**

function multiplyAll(arr) {
let product = 1;
// Only change code below this line
for (let i = 0; i < arr.length; i++) {
for (let j = 1; j < arr[i].length; j = arr[0] * arr[1])
}
// 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62

Challenge: Nesting For Loops

Link to the challenge:

So I’ll just go through what you want your iterator variables (i and j) to be for the example input of [[1, 2], [3, 4], [5, 6, 7]].

i: 0, j: 0
i: 0, j: 1
i: 1, j: 0
i: 1, j: 1
i: 2, j: 0
i: 2, j: 1
i: 2, j: 2

You need to work on your inner for loop so that j aligns with those values above. Also, you actually need some code to execute inside the inner loop…

Your second/inner loop doesn’t seem right to me.
You don’t need complex logic here, try console.log inside the inner loop once you get/see all the values in the console you can multiply them.

Hi!
First of all the third arugument of your 2nd loop should not be j = arr[0] * arr[1] , instead try j++. The reason is that the final expression is mainly used to increment or decrement counters.

Secondly, these loops execute only if you pass some code into your nested loop.
I would suggest using the *+ (for multiplying) operator and assigning it to the product variable

I don’t know that the Javascript interpreters are actually smart enough to skip loops with empty bodies.

Thank you all for your input! Here is what I’ve worked so far, even redoing the previous exercises. I think this is improved but I know I’m still missing some thought process on this :sob:

Here is what I have:

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


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

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

The console only says “j is not defined”

I know the first “i” part makes the function execute for the larger groups, while the second part executes each number within the loop. I thought I was making the numbers multiply each other by putting:

product *= arr[i][j];

but that’s obviously not doing anything :confounded:

Where do I go from here?

You’re missing curly brackets around the body of your inner loop.

:rofl:
It cracks me up what actually stops code from working!

Thank you all so much for your input! I’m cycling from banging my head on the wall, to feeling accomplished, and back-and-forth, lol. My own loop.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.