Need help with this js challenge

Tell us what’s happening:

Can somebody help me with this I don’t know what is happening and am all confused i just dont know what to do!

Your code so far


function multiplyAll(arr) {
var product = 1;
// Only change code below 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]]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36.

Challenge: Nesting For Loops

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

The goal is to multiply the product variable (given as 1) by all of the input numbers:

multiplyAll([[1,2],[3,4],[5,6,7]]); should return in 5040 ( 1 * (1*2*3*4*5*6*7) )

Your input is an Array of arrays, and the challenge is working through all inner arrays one by one to get the next multiplier. The test is named “Nesting For Loops” so hopefully that gives you a hint :slight_smile:

1 Like

I understood a bit but my confusion is what the loops are doing

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]); } }
what are these lines of code doing exactly?

Is the first For loop for (var i=0; i < arr.length; i++) adding 1,2,3 to i? like i = 3 ? Arrays start from 0 right? im really not getting it

1 Like

Yes, arrays start at 0 index,
Here is the sample given and I’ve added some empty lines (and changed the numbers).

var arr = [ [51,52], [53,54], [55,56] ];

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

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

}

What does arr[0] = ?
If you wanted to isolate 54 from arr what would you use? arr??

1 Like

arr[1][1]? Like this?

So the console would be 54 right?

1 Like

Ya. Exactly.

so: console.log(arr[i][j]) in the example above: when i=1 & j=1, console is 54.

The trick is to figure out the double loops to get all of the numbers

1 Like

Oh! I feel like i’m getting this

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

So in this example the initial var i = 0 is [1,2] right? And j = 0 means 1 right? Inside [1,2]?

1 Like

Correct!
arr[i] = [1,2] when i=0
& [1,2][j] = 1 when j=0

1 Like

Thanks for clearing that confusion

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

Can you tell me what’s happening here?

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

What does arr[i] do?

Also, i < arr.length means less than the array length right?

So [1,2], [3,4], [5,6] is in total = 3?

But we are searching for the index right? Like 0, 1, 2.

does i = 0 ; i < arr.length output the index? Like 0, 1, 2 or the number of arrays like 1, 2,3?

1 Like

Correct; it comes from the main function:
function multiplyAll(arr) {...
And this example has 3 items like you said.

Remember that you have 2 loops.

arr[i] is the first loop… i = 0, then i = 1 … etc…

say arr=[[9,8,7,6,5,4,3],[19,18,17,16,15,14],[29,28,27,26,25]]
and i=0 (first pass)
what is arr[i].length ?
and then where does the j come from? And how does that relate back to arr[i] ?

1 Like

say arr = [[9,8,7,6,5,4,3],[19,18,17,16,15,14],[29,28,27,26,25]]

i = 0;

arr[i] is arr[0] which is [9,8,7,6,5,4,3]

so is arr[i].length counting the numbers? or the number of items in the array?

there are 7 items in the array [9,8,7,6,5,4,3] is that the output?

I’m really confused regarding this part

1 Like

correct above and that arr[0].length is 7.

The goal of this and other objects is to break it down into steps.

You started with a big array with many arrays numbers. But, now we have broken that down into just a single array.

Start: arr = [[9,8,7,6,5,4,3],[19,18,17,16,15,14],[29,28,27,26,25]]
First loop; i=0: `arr[0] = [9,8,7,6,5,4,3]

Now time for the second loop, j … Same process, break this array down into its parts, arr[i][j]

1 Like

okay so arr = [ [9,8,7,6,5,4,3],[19,18,17,16,15,14],[29,28,27,26,25] ]

i = 0 is [9,8,7,6,5,4,3]
i = 1 is [19,18,17,16,15,14] 
i = 2 is [29,28,27,26,25] 

J = 0 is 9?
J = 1 is 8?

am i trying to find the length of the array or outputting the numbers inside?

1 Like

You are getting close, but remember the challenge is nesting the loops, so arr[0][1] = 0 , arr[0][1] = 8 … and you have to go through all of the numbers (that’s why we use the loops the computer does it for us :slight_smile: )

You are trying to grab each number inside and use them to multiply together.

The challenge is multiply the product variable (given as 1) by each number in the sub-arrays of arr

so product=1 * arr[0][0] * arr[0][1] … arr[?][?]

1 Like

This is really confusing Im trying really hard to understand but looks like i need to start fresh again

1 Like

It is confusing at first (and always) but just keep at it :slight_smile:

If you are more of a visual learner, copy the code below that we made into the link. It will step through each process of the code and you can see how the j loops through i and hopefully that will help

var arr = [
  [9,8,7,6,5,4,3],[19,18,17,16,15,14],[29,28,27,26,25] 
];
for (var i=0; i < arr.length; i++) {
  for (var j=0; j < arr[i].length; j++) {
    console.log(arr[i][j]);
  }
}

http://pythontutor.com/javascript.html#mode=edit

1 Like

i will check it out! thanks a lot for the help :slight_smile: <3

1 Like

I used the Visual learner then found out that it loops till it outputs all the numbers in the array then it goes up to i and changes its position from i = 0 to i = 1 so far I understand this but I’m still confused about the code I will keep trying till I get the answer i need thanks for your help!

1 Like

So i understand this now


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



first run when i = 0 and j = 0 it will select 1 from [1,2]

it will do the same till the end of the length of the array

and then the loop will break

product = product *= arr[i][j] ; will multiply the array element with the product which is 1

1 * 1 = 1 is arr[0][0] targeting first 0 will look at [1,2] and then second 0 will inside [1,2] and select 1

and the selected 1 is multiplied by the product which is 1 after that the result of the first loop will be added to the product 1 * 1 = 1 , 1 * 2=2, 2 * 3 = 6 on and on till the end of the arr and the final result will be 5040

A couple of things.
product *= arr[i][j];
Already puts the value back into product so another product = is not needed;
The above is the same as:
product = product * arr[i][j];
so your code is doing, essentially:
product = product = product * arr[i][j];

I find for starting out it might be easier to visualize if you use another
variable for the subArray (or inner Array) and loop then you don’t have
the initially confusing arr[i][j]

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