freeCodeCamp Challenge Guide: Nesting For Loops

Though I understand this code/solution, I am unsure as to why we must console.log(); the multiplication in step 3. Why can’t it be returned? return (product*=arr[i][j]);? I guess it would have no place to store the returned value or is there a way to set return product = product*=arr[i][j];?

2 Likes

return is only supposed to output one number when all is said and done. Doing this for every iteration of the loop would result in multiple returns.

I just tried, and you don’t necessarily have to use console.log. You can just write:

product *= arr[i][j];

…and the result will still pass.

2 Likes

You don’t want to return if because you will not be able to reach the return product at the bottom of the code. The console.log() is unnecessary though. For me the third line was simply product *= arr[i][j].

Hope this helps!

4 Likes

So in this case the nested loop is just there to loop through the numbers in the nested array, and once we have the # of arrays of the values within those arrays we can multiply as we loop through…I think I understand it now. Thanks for translating this into easy human speak :slight_smile:

1 Like

Thank you for the break down. Now I see how the arrays would sum up to the final result.

Just one question, why arr[i][j] *= product does not works? Is it not supposed that factors order doesn’t change the product? :S

bcz it means a different thing when u code like that…product =arr[i][j] means…u assign product a new value and then update it… however, when arr[i][j]=product, the “product” is always 1 …

you have a keying error you misspelled “.length”.

Sorry about the previous message, glad to hear you got it. (:

You need to multiply, rather than add , hence *= instead of +=

I sat here for 10 minutes wondering why I won’t pass. Turns out I wrote greater than (>) instead of smaller than (<). But that’s the fun part, isn’t it? Figuring out your mess and finally getting it to work.

Your code should look similar to this (this code passed as successful):

Count through the outer arrays (there’s 3 of them, so the loop will count 0, 1, 2)
for(var x = 0; x < arr.length; x++) {
Count through the inner array of each outer array
for(var y = 0; y < arr[x].length; y++) {
Now with each loop, multiply the next value of the array and store the result in product
product *= arr[x][y];
}
}

In the last step the parser will work like this:

Loop 1:
product *= 1 (first value in array 0) & 1 (given starting value of var product) = 1
Loop 2:
product * = 1 (product of last loop) * 2 (second value in array 0)
Loop 3:
product * = 2 (product of last loop) * 3 (first value in array 1)

and so on and so forth … hope this makes it a little easier to understand

18 Likes

Let’s say this is your array:

var LiciniusRex[1,2,3,4,5,6] - then LicinusRex.length would return 5 (keep in mind, the array starts counting the elements with 0 instead of 1).

So with our loops we are telling the parser:

for as long as the counting variable e. g. var i is smaller than the amount of elements in the array, keep on repeating the loop.That’s why we set the initial value of the counting variable to 0 and then at the end of the loop increase it by 1, as long as this condition hasn’t been met.

1 Like

I have a question about that loop and the way the function works:

for (var i=0; i < arr.length; i++) {
for (var j=0; j < arr[i].length; j++) {
product *= arr[i][j];
}
}

Does the function wait for the nested sub-loop (j) to execute as many times as possible ( < arr[i].length ) BEFORE incrementing the main loop (i) ?

So for example if there is 3 arrays with 3 sub-arrays each - product *=arr[i][j] does :
arr[1][1] * arr[1][2] * arr[1][3] * arr[2][1] * arr[2][2] * arr[2][3] * arr[3][1] * arr[3][2] * arr[3][3]

Or it goes through another methodology?

6 Likes

yes it does it that way

1 Like

Yeah how to push the second for onto the second digit was confusing!

1 Like

Thank you. Not enough words were given in the example for me to have any understanding until I found this.

1 Like

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

  1. Initialize: var i=0
  2. Test: i < arr.length; Is i=0 less than array.length(3). If YES or TRUE enter inner loop

INNER LOOP

  1. Initialize: var j=0
  2. Test: j < arr[i].length;
    ->i was initialized to 0.
    ->array[i].length is same as array[0].length i.e length of [1,2] which is 2).
    ->Is j=0 less than array[i].length? YES, execute code inside j loop
    product= product(=1) * arr[i][j](value in position [0][0] (=1))
    product= 1
  3. Increment: j++ (j=1)
  4. Test: j < arr[i].length;
    ->i is still initialized to 0.
    ->array[i].length is same as array[0].length i.e length of [1,2] which is 2).
    ->Is j=1 less than array[i].length? YES, execute code inside j loop
    product= product(=1) * arr[i][j](value in position [0][1] (=2))
    product=2
  5. Increment: j++ (j=2)
  6. Test: j < arr[i].length;
    ->i is still initialized to 0.
    ->array[i].length is same as array[0].length i.e length of [1,2] which is 2).
    ->Is j=2 less than array[i].length? NO, go to outer loop.
  7. Increment: i++ (i=1).
    Repeat process from Outer Loop with i=1. Then…
    product= product(=2) * arr[i][j](value in position [1][0] (=3))
    product=6
    and so on…hope this helps
49 Likes

I handled it like this:

function multiplyAll(arr) {
var product = 1;
// Only change code below this line

for(var i = 0;i < arr.length;i++) {
for(var k = 0;k < arr[i].length;k++) {
product *= arr[i][k];
}
}

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

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

1 Like

Just so i understand… the letters you use to identify the outer and inner arrays are pretty arbitrary right? Like i could use any two letters and the loops will identify the arrays anyway.

6 Likes

Yes. They’re variables created for the loop to identify the array contents.

2 Likes

This was my solution:
function multiplyAll(arr) {
var product = 1;
// Only change code below this line
for (n = 0; n < arr.length; n++) {
for (m = 0; m < arr[n].length; m++){

product = product * arr[n][m];

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

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

I didn’t use console.log and it worked. I know this is basic but I would like to know why, thanks.