Basic JavaScript - Nesting For Loops

Tell us what’s happening:
Describe your issue in detail here.

Your code so far
well I cant seem to understand this, I put in the console.log to see what is going on.
Loop one is on: 0, Loop two is on 2
Loop one is on: 1, Loop two is on 2
Loop one is on: 2, Loop two is on 3
I don’t understand why the loop 2 begins at 2 = that is why I am not passing the test but what am I missing. Some misunderstanding on my part.
Can someone give me the long explanation.
been going pretty good up until now.

function multiplyAll(arr) {
  let 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("Loop one is on: "  + i + ", Loop two is on  " + j)
   
} 

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

  // 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/111.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Nesting For Loops

Link to the challenge:

That semicolon at the end is definitely going to cause a problem. Notice how your outer for loop uses curly braces to enclose the code you want to run inside the loop.

for (var i = 0; i < arr.length; i++) {
  // code to run each time through the loop...    
}

Your inner for loop needs to do the same thing.

The arr[i],[j] isn’t quite how you access a value in a multi-dimensional array. There is no need for a comma.

Also, I think you want that to go inside of your loops, don’t you?

Hi there, it looks like the problem is that your console.log is after a semicolon ending your inner for loop. Instead of the semicolon, consider using { } to encole your console.log so it is run each time the inner loop is run. I hope this helps

function multiplyAll(arr) {
  let 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++);
   product *= arr[i][j];
} 
  // Only change code above this line
  return product;

}

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

ok I have read alot and took your coaching but my coding still fails the test.
returns this
// running tests

multiplyAll([[1], [2], [3]])

should return

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

should return

5040
multiplyAll([[5, 1], [0.2, 4, 0.5], [3, 9]])

should return

54

// tests completed
LOL clearly there is still something I don’t understand.

The semicolon on the first line is a problem.

Switch out var for let and you will see an error. That is because the ; on the first line is saying that your loop has no body. Using var ‘covers up’ this error.

2 Likes

Thanks Jeremy: always syntax troubles I have. I suspected but did not know what I was looking for.

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