Basic JavaScript - Nesting For Loops

Tell us what’s happening:
please help me understand why there are only i and j and not k (only 2 elements in the array) but not third one as we see 3 array elements in the given question even in the sub array.
second is i dont understand this j<arr[i].length .
Describe your issue in detail here.

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=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]]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Nesting For Loops

Link to the challenge:

i represents index of the sub arrays. In the case of:

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

there are 3 sub arrays. The first one [1, 2] would arr[0]. The second one [3, 4] would be arr[1] and the third one [5, 6, 7] would be arr[2]. i would have the values 0, 1, and 2 in the first for loop.

The index of the elements of each sub array are represented by j.

The first sub array would use the values 0, and 1 for j representing the values 1 and 2 in the first sub array.

arr[0][0] // 1
arr[0][1] // 2

The second sub array would use the values 0 and 1 for j representing the values 3 and 4 in the second sub array.

arr[1][0] // 3
arr[1][1] // 4

The third sub array would use the values 0, 1, and 2 for j representing the values 5, 6, and 7 in the third sub array.

arr[2][0] // 5
arr[2][1] // 6
arr[2][2] // 7

thank you for the reply.
in what conditions can i use for (let i=0;i<arr.length;i++)
{
for (let j=0; j<arr[i].length; j++)
{
for(let k=0; k<arr[j].length; k++){
product*=arr[i][j][k]; }

orrrrrrr the below
for (let i=0;i<arr.length;i++)
{
for (let j=0;j<arr.length;j++)
{
for (let k=0; k<arr.length; k++)}
product*=arr[i][j][k];
}

When ever you write arr[i][j][k], you must have an array with 3 dimensions. The catch is how you write the conditions of the for loops themselves.

For your second example of:

function multiplyAll(arr) {
  product = 1;
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr.length; j++) {
      for (let k = 0; k < arr.length; k++) {
        product *= arr[i][j][k];
      }
    }
  }
  return product;
}

this would allow you to make the following call. Since all of the iterable variables depend on how many elements are in arr, each sub array needs to have the same number of elements as arr for it to successfully generate a number. In the following case, the main arr has two elements. Each of those elements are arrays that have two elements.

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

For your first example of:

function multiplyAll(arr) {
  product = 1;
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i].length; j++) {
      for (let k = 0; k < arr[j].length; k++) {
        product *= arr[i][j][k];
      }
    }
  }
  return product;
}

I was not able to think of a practical example other than using the same call as the first example. The j < arr[i] and k < arr[j] puts unique constraints on the array structure. There are other arrays that could possibly work too, but I did not spend much time on thinking about them.

A more practical version of the function would allow the inner sub arrays to be of varying length. See if you can create a function that could successfully calculate the product of the following array using nested for loops.

[
  [
    [1], [2, 3]
  ],
  [
   [4], [5, 6, 7, 8]
  ]
]

The product should result in the value 40320 for the above array.

1 Like

Thank you sir for your precious time and for the explanation.