Nesting for loops has me buffaloed [SOLVED]

Nesting for loops has me buffaloed [SOLVED]
0.0 0

#1

Hello,

Welcome to the new forum I guess. This is my first venture in the new format so bear with me.

I am working through the lessons in order in the FreeCodeCamp curriculum and I have come upon a lesson on nesting for loops that is giving me fits. I don’t want so much to be fed the answer but I need an explanation of what’s going on here.
So, Here’s my code:

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 = product * ?????;
 
  // Only change code above this line
  return product;
}

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

What I understand is that there is a function called multiplyAll which takes an arguement(arr) I understand the nested for loops I think. The argument (arr) actually takes more than one paramenter or argument: like this [i,j] So the first for loop finds the i’s and the second one finds the j’s and and the console.log statement is supposed to multiply i and j together and multiply those products. Or I’m wrong about that.
In the answer to the problem product needs to be equal to all the products multiplied together. But the product= statement has me buffaloed.
So if you could help with the answer and an explanation of what I’m missing I’d be grateful
All the best,
luciano991


#2

I’ve not done this lesson, so I’m confused about what you’re trying to achieve with the line:

product = product * ????;

However, I might be able to help clarify the argument passed into “multiplyAll” which may in turn help you understand that product line.

So, “arr” in this instance is a Multidimensional Array, or Array of Arrays. The first for loop says, “Okay, we have an array with 3 elements, lets loop through those.” At this point, it doesn’t know or care that each element in the array is in fact another array. Now the second for loop grabs the element retrieved by the first for loop and says, “Okay, I know you’re an array, lets loop through each element.”

So what I think you want to happen here is put that product assignment line where your console log is currently.

product *= arr[i][j];


#3

Hello @luciano991, I’m going to try to explain what it does. I think you are a bit confused.

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]); // <--------- This line does nothing but log 1 then 2 then 3 until 7.
      //What you need to do is multiply here instead of logging. As suggested:
      product *= arr[i][j]; //The i gives us the i array in the main array. (ex: first one would be [1,2])
      // And the j gives the elements inside the array[i] (ex: first one would be 1)
    }
  }

  //product = product * ?????;  <------- Commenting this line

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

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

If you need further examples feel free to tag me, I’ll be happy to help. (maybe I’ll modify my reply depending on how it renders.)

It’s a bit confusing but remember that [ ] means there is an array, so if you have [ [ ],[ ],[ ] ] that would be an array with 3 arrays inside. Hope this helps.

Cheers!
Daniel S.


#4

Thank you Daniel. Your explanation was indeed helpful. And I made the mistake of putting product *= arr[i][j]; outside of the function. Otherwise it mostly makes sense. Baby steps.

All the best,

Luciano991


#5

Yes, exactly. The ??? was to indicate that I didn’t know what to put there. :smiley:

I also had placed the product *= arr[i][j]; outside of the function. Rookie mistake.

Thanks for your time and trouble.

Appreciate the explanation.

Luciano991


#6

Excellent @luciano991, the important thing is that you grasp the idea. If it is clear to you, I think it will help all campers if you edited the subject of your post and added (SOLVED) at the end. If you are still having trouble grasping the concept, I would gladly make several examples for you.

Cheers,
Daniel S.


#7

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.


#8

Thanks. I knew triple ticks worked in the chat but I didn’t know they worked in the forum.

Mark


#9

How did you get var i and var j? I don’t understand where these two variables are coming from.


#10

Hey @pcabrera83,

They are made up variables, it’s just to store the Index of the loop. It’s common to use i since it’s shorter than index, and j since it’s the next letter alphabetically.

I’m guessing you are having trouble with loops, if you have any questions about it let me know. It’s crucial you understand how to loop in arrays and nested arrays in programming! So don’t hesitate to ask.

Cheers,
Daniel S.


#11

this is where i feel like having a conversation with someone in person would help. I live in a small town and the closest FreeCodeCamp group is about 2 hours away.

using “j” just blows my mind because “j” is not mentioned in the code itself but for some reason it is used and I don’t know why.

and where does .length even come into the code?

and how would one know that

product *= (arr[i][j]);

1.) *= what does this mean?
2.) what does arr[i][j]) mean and how would one know to input this?

I mean if anything maybe I would put

(arr[i]*[j])


#12

using “j” just blows my mind because “j” is not mentioned in the code itself but for some reason it is used and I don’t know why.

It’s not in the code? It looks like it is to me.

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

There is nothing magical about i or j or whatever. The standard for loop is:

for (var i = 0 ; i < 10 ; i++ )

But i is arbitrary. It probably stands for “iterator”, but it could have been any variable name:

for (var elephantPajamas = 0 ; elephantPajamas < 10 ; elephantPajamas++ )

But people usually use i by tradition if they need a vanilla iterator in a for loop. And if i is taken, they use j, then k, etc. But you can use something else if it makes it clearer.

1.) *= what does this mean?

product *= (arr[i][j]) is the same as saying product = product * (arr[i][j])

If you write a *= b*, that is saying "multiply a times b and assign it back into a, but you can write a = a * b if you want. But you’re going to see that a lot so you’d better get used to it. I think that it’s also technically a little faster.

2.) what does arr[i][j]) mean and how would one know to input this?

arr is an array of arrays, as defined in the code, multiplyAll([[1,2],[3,4],[5,6,7]]);

For example, if i and j are such that you are looking at arr[2][1], then that would evaluate as 6, because you are looking for the 2 element in the top level array ([5,6,7], remember that we start counting with 0) and within that you are looking for the 1 element (6, remember that we are counting from 0).

The code is just using arr[i][j] to step through all the possible cells.

If that is not clear, then I’d do some research on arrays, especially multidimensional (nested) arrays. Don’t get frustrated - we all have stumbling blocks. This is a little weird until you wrap your head around it and then it’s easy.

Look for some tutorials. Youtube might be good because it is so visual.

https://www.youtube.com/results?search_query=javascript+nested+arrays


#13

thank you for the excellent guidance and support! It means a lot to me.