freeCodeCamp Challenge Guide: Nesting For Loops

freeCodeCamp Challenge Guide: Nesting For Loops
0

Nesting For Loops


Problem Explanation

Relevant Links


Hints

Hint 1

Make sure to check with length and not the overall array.

Hint 2

Use both i and j when multiplying the product.

Hint 3

Remember to use arr[i] when you multiply the sub-arrays with the product variable.


Solutions

Solution 1 (Click to Show/Hide)
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 = 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]]);

Code Explanation

  • We check the length of arr in the i for loop and the arr[i] length in the j for loop.
  • We multiply the product variable by itself because it equals 1, and then multiply it by the sub-arrays.
  • The two sub-arrays to multiply are arr[i] and j.
2 Likes

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

4 Likes

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

12 Likes

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

20 Likes

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.

7 Likes

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];
}

}

4 Likes

I found a mistake. My eyes are blind …

7 Likes

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
2 Likes

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];
}
}

17 Likes

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.

5 Likes

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

8 Likes

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?

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

2 Likes

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

22 Likes

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

2 Likes

Thank you for your response!

1 Like

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]]);
3 Likes

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?

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.

1 Like

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).

27 Likes