Basic JavaScript - Nesting For Loops

Tell us what’s happening:
I don’t understand how this code is read, I only understood how it got the product but the process of getting the information inside the array is still confusing for me.

Your code so far

function multiplyAll(arr) {
  let product = 1;
  // Only change code below this line
  for (let a = 0; a < arr.length; a++) {
    for (let b = 0; b < arr[a].length; b++) {
      product = product * arr[a][b];
    }
  }

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

console.log(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/105.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Nesting For Loops

Link to the challenge:

Can you be more specific about what is confusing you?

the process of getting the information inside the array is still confusing for me

By “getting the information inside”, do you mean placing it or retrieving it? That could read either way.

In the first sense, the function is called with [[1, 2], [3, 4], [5, 6, 7]] and that gets stored in the variable arr.

If you mean in the second sense, we need indices so we can access the variable as arr[a][b]. So, we need valid row numbers for a and valid column numbers for b (if we want to visualize our array like that). So this:

for (let a = 0; a < arr.length; a++) {

will give us the row numbers, one by one.

And this:

    for (let b = 0; b < arr[a].length; b++) {

will take each row (arr[a]) and index through the total number of columns in that row (arr[a].length).

1 Like

I added [spoiler][/spoiler] tags since that is a working solution to a curriculum challenge.

It was the second answer but I’m still a bit confused, what do you mean on the first code “will give us the row numbers”. I don’t know what it will take or how I should visualize that.

He means that as your input is like a table (or a 2 dimensional array), the first loop is like a finger pointing at each sub array (it gives the index of the sub array inside the larger array, which can also be imagined as rows in a table)

1 Like

So the first code is looking at how many arrays are there inside the main array?

Uh-huh, correct. :white_check_mark:
(I can’t make a short post so I have to write all this)

what about the second part of the code? What did he mean by “will take each row (arr[a] ) and index through the total number of columns in that row (arr[a].length).”

Remember it is an array of arrays or 2 dimensional array? So you can imagine it like a table.
To read a table you go thru each row (first loop) and you read each column in the row (so that part needs a second loop).

1 Like
const table = [
  [1, 2, 3],
  [4],
  [5, 6],
  [7, 8, 9, 10],
]

const numberOfRows = table.length

for (let rowNum = 0; rowNum < table.length; rowNum++) {
  console.log('* rowNum =', rowNum)

  const thisRow = table[rowNum]
  console.log('* row', rowNum, 'is', thisRow)

  const numberOfColsForThisRow = thisRow.length

  for (let colNum = 0; colNum < thisRow.length; colNum++) {
    console.log('*** colNum =', colNum)
    console.log(`*** cell [${rowNum}, ${colNum}] is ${table[rowNum][colNum]}`)
  }
}
1 Like

Hi my friend!!

You could watch this video

he is explaining everything, with fun :sunglasses:

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.