freeCodeCamp Challenge Guide: Nesting For Loops

freeCodeCamp Challenge Guide: Nesting For Loops
0

#1

If you have a multi-dimensional array, you can use the same logic as the prior Challenge to loop through both the array and any sub-arrays. Here is an 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]);
      }
    }

This outputs each sub-element in arr one at a time. Note that for the inner loop, we are checking the .length of arr[i], since arr[i] is itself an array.


What is the point in logging a for loop to the console and could you complete the challenge without doing so?
#2

I’m stuck trying to solve this challenge… anyone give me a hand with it??


#3

Hi,

Struggling to get my head round this nested For Loops Challenge. Please can someone explain how the arr[i] works checking the inner loop i.e.

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

how does this work? I’m a bit lost and don’t want to advance until I get all these concepts.

Thanks

Raoul


#4

You are supposed to multiply the elements in the array and pass the value to product like so:
unction multiplyAll(arr) {
var product = 1;
// Only change code below this line
for (var p=0; p < arr.length; p++) {
for (var s=0; s < arr[p].length; s++) {

   console.log(product *= arr[p][s]);
 
}

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

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


#5

This is easier to understand if you actually include the array variable on the right. It’s there on the left, but it’s not clear that it is the same for the ‘test’; It’s easier to understand if it is included.

So, instead of:
function multiplyAll(arr) {
var product = 1;
// Only change code below this line

It should be:

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

Edit: Deleted the numbers. Not sure if it’s correct. Anyway, remember that the array is arr, not multiplyAll. That is confusing - or at least it was for me.


#6

Hi all!
Pleasa, tell my anybody why my code doesn’t work???

for (var i=0; i < arr.lenght; i++) {
for (var a=0; a < arr[i].lenght; a++) {
  product += arr[i][a];
}

}


#7

I found a mistake. My eyes are blind …


#8

By modifying the values, I was able to pass this challenge. If the change the vales above, by removing 4, 5, 6, 7, 9 etc…, how do we suffice the other two requirements seen below?

multiplyAll([[1,2],[3,4],[5,6,7]]) should return 5040
multiplyAll([[5,1],[0.2, 4, 0.5],[3, 9]]);) should return 54

#9

Took me a little bit to sit there and think through what i was trying to do but here it is.
You are going into the main Array with the first FOR then going into the inside Arrays with the second FOR.
then just multiply the running product with the value you have found.

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


#10

I really don’t understand how this works. I’m confused by the use of .length. Can someone link me to something that goes into depth explaining how it works? Or can take a few minutes to explain it to me? Feeling lost and don’t want to move much further without understanding it.


#11

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

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

ps:The first loop grabs the leghth of the outer array
The second grabs the sub arrays
the outer array and sub arrays are connected by arr[i] that’s how I understand


#12

Hi all, I have completed the challenge but I have something I can’t understand. When my code finish the first iteration and get the first value from the first array, why it still working on the nested for loop again instead of going to the top level loop?


#13

It works on nested for loop until it is false and then moves to top for loop.


#14

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

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

.length gets the length of the array, in this case it is =3 i.e [1,2], [3,4], [5,6,7].
Arrays are zero index based i.e 0 index goes for [1,2], 1 for [3,4], 2 for [5,6,7].
now in the for loop we are giving a condition i<arr.length i.e i<3, and array indexes are 0,1,2 which are less than length-3.
there for the loop executes until condition i<3 is false.

https://www.w3schools.com/js/js_arrays.asp


#15

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

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


#16

Thank you for your response!


#17

solution

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

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

#18

So with this code I just want to confirm this will only multiply arrays within the initial array so if you was to add another number you will either have to add it to the sub array or create a new sub array, correct?


#19

multiplyAll([[1,2],[3,4],[5,6,7]]);
Okay bear with me. How does the code above output 5040? And for that matter, how do the other two come up with that conclusion. I pretty much obtained some understanding in how the for loops work. But when modifying the call to function using arrays, how is it that the FCC comes up with those answers.


#20

Here is what the finished code basically says (inside the loop, only):

  1. Loop through each item in the array (there are three originally, but each item is an array):
    for (var i = 0; i < arr.length; i++)

  2. Loop through each nested array:
    for(var j = 0; j < arr[i].length; j++){

  3. Product = Product*arr(and do this across arrays):
    *console.log(product = arr[i][j]);

This is the same as 1234567 (12=2; 23=6; 64=24; 245=120… etc. eventually 5040)

Originally, product = 1 (see original code). The first thing in the array is 1. So 11=1 (product still equals 1). Next in the array is 2, so 12 (product*2) = 2. This goes on and on until it loops all the way through the array(s).