Create more than two arrays

I wanted to create another inner loop, so I did this, but didn’t wok out, Why’s this happening?


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++){
  for(var k=0;k < arr[i][j].length;k++){
    product += arr[i][j][k]
  }
}
}
// Only change code above this line
return product;
}

console.log(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/92.0.4515.131 Safari/537.36

Challenge: Nesting For Loops

Link to the challenge:

Outer loop goes through the outer array. For each inner array, the inner loop goes through it. Then you have another loop which tries to loop through each individual value. They’re numbers, you can’t loop through a number, it’s just a number, so the function breaks when you try to that.

2 Likes

So in the first pass of your looping, i==0 and j==0. arr[0] is that first sub-array, and arr[0][0] [because remember, both i and j are 0] is 1 - the first element of that sub-array. And 1, being of type Number, doesn’t have a length.

1 Like

He beats me out again lol

2 Likes

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