Nesting For Loops // please explain!

Nesting For Loops // please explain!
0

#1

Tell us what’s happening:

Your code so far


function multiplyAll(arr) {
  var product = 1;
  // Only change code below this line
  for (var x=0;x<arr.length;x++){
    for (var y=0;y<arr[x].length;y++){
      product=[x]*[y];
    }
  }
  // Only change code above this line
  return product;
}

// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/nesting-for-loops


#2

What exactly do you think the line above is doing?


#4

we need to return product in the end , so product should be the x * y … which should be each number in the array!


#5

I have difficulty understanding how the inner loop works though! could you plz explain?


#6

think about this though, x is getting you part of the array and y is getting you another part of it. you want to multiply each part of the array with the variable product.


#7
product*=[x][y];

?


#8

the above array contains 3 arrays, the first for loop runs through those 3. the nested for loop checks each element in each of those three arrays.


#9

I will tell you what the JavaScript interpreter thinks you wrote.

Starting on the right side of the assignment operator, you are multiplying an array with one element who’s value is x by an array with one element who’s value is y. Then you are assigning that value to product.

Because you are replacing the value of product in each iteration of the inner for loop with this, only the last inner for loop iteration will determine the final value of product.

First, think about what the values of x and y are on any given iteration. The are index values of arrays and NOT the actual values of the arrays.

Second, you do not want to replace the value of product with just the product of two number. Instead you want to multiply the current value of product by the current subarray element AND THEN assign the result back to product. To refer to the inner subarray, you would write arr[x][y].

See if you can take it from here.


#10
product*=arr[x][y]

so just to clarify how javascript interpret the code,
x will be the 1st number and goes to 2nd loop y will be 2nd number and if the array ends it goes back to x in the next array. BUT if the array has 3 number it will be x,y,y??


#11

If you had a three-dimensional array, then to reference the inner most array, you would reference as:

arr[x][y][z]


#12

but I didn’t make var z.

so for this array [[1,2],[3,4],[5,6,7]]

how does my code read?


#13

I thought you were asking about a third array dimension. The array below is just a two-dimensional array. You will only use x and y, that is why your product *= arr[x][y] works.

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

It has two levels. The first level is the outer array which three elements (which are also arrays [1, 2] , [3, 4], and [5, 6, 7]). The second level is the 3 sub arrays.


#14

ok, I think see how 2 dimensional array are. But what am trying to find is how the written code goes through the array.
for example,

var arr = [
  [1,2], [3,4], [5,6]
];
for (var i=0; i < arr.length; i++) {
  for (var j=0; j < arr[i].length; j++) {
    console.log(arr[i][j]);
  }
}

i will the 1st element of each secondary array; 1, 3, 5 and j is the 2nd, 2, 4, 6.
is that correct?


#15

think of it this way,
x and y are two guys going through baskets together.

x is in charge of the outer array, the three big baskets, each time he picks up a basket he gives it to y.

y then looks at each item inside the basket…

and assuming you have your multiplier set correctly, multiplies each item by the number contained in product and makes the product of that multiplication the new value of the variable product.

Each time y finishes a basket the focus switches back to x. in this way every number ends up being counted.

so when we type arr[x][y] within those for loops, what we are really saying is the basket that x has most recently picked up and the item within that basket that y is currently looking at.


#16

No, i represents the index of the each subarray in arr. j represents the index of each element of each sub array.

If I modify your console.log, I think it will be clearer.

var arr = [
  [1,2], [3,4], [5,6]
];
for (var i=0; i < arr.length; i++) {
  for (var j=0; j < arr[i].length; j++) {
    console.log(`arr[${i}][${j}] = ${arr[i][j]}`);
  }
  console.log();
}

The above displays the following in the console:

arr[0][0] = 1
arr[0][1] = 2

arr[1][0] = 3
arr[1][1] = 4

arr[2][0] = 5
arr[2][1] = 6