Nesting For Loops where did the j come from

Got the code but why do we need j and i.

Your code so far

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0.

Link to the challenge:

You have an array of arrays, so you need arr[i] to access one of the sub-arrays, then to get an element from the sub-arrays you need a second index

In your code arr[0] is [1,2] and arr[0][0] is 1

If you are asking why that name to the variables… well, you don’t need to use those, it’s just that those are usually used

One way to make it clearer might be to write this as:

for (var arrIndex = 0; arrIndex < arr.length; arrIndex++) {
  var subArr = arr[arrIndex];
  for (var subArrIndex = 0; subArrIndex < subArr.length; subArrIndex++) {
    product = product * (subArr[subArrIndex]);
1 Like

I’m have a very hard time with this I’m glad you guys are there.

Nested arrays were confusing to me the first time I worked with them too. Two-dimensional arrays are used a lot, so the more you work with them, the easier it will become.

If you have ever worked with a spreadsheet program like MS Excel or Google Sheets, you could think of a spreadsheet as a two-dimensional array. The rows would contain the sub arrays (columns) of the main array (the spreadsheet).

var spreadsheet = [
  ['RandellDawson', '1/17/2017', 'online'],
  ['royalgreen50', '3/10/2019', 'online'],
  ['fccUser', '10/22/2018', 'offline']

for (var rowIndex= 0; rowIndex < spreadsheet.length; rowIndex++) {
  console.log('Row #' + (rowIndex + 1));
  var columns = spreadsheet[rowIndex];
  for (var columnIndex = 0; columnIndex < columns.length; columnIndex++) {
    console.log('Column #' + (columnIndex + 1) + ' value: ' + columns[columnIndex]);

The above would display the following in the console.

Row #1
Column #1 value: RandellDawson
Column #2 value: 1/17/2017
Column #3 value: online
Row #2
Column #1 value: royalgreen50
Column #2 value: 3/10/2019
Column #3 value: online
Row #3
Column #1 value: fccUser
Column #2 value: 10/22/2018
Column #3 value: offline

I need to copy that, Please.

@royalgreen50, in case you don’t know this, you can bookmark his response. Click on the elipsis (…) and then click on the bookmark tab. You can always get to it again by clicking on your icon and clicking on the bookmark tab there. It will show you all of your bookmarks and you’ll be able to read the full thread.

1 Like

OK good to know I struggling with java and i love coffee go figure.