Please help me understand a part of loops and .length

In this code:

function multiplyAll(arr) {

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

multiplyAll([[1,2],[3,4],[5,6,7]]);

I have got all of the numbers assigned to multiply by each other to produce a value, but I do not understand how I got there exactly. What meaning does .length provide? I thought that .length gathered the number of variables in an array and totaled it, but the culmination of all of these variables comes out to 5040. When I am using a length proper in my condition piece of the "for loop ", how exactly does it work?
Also, under my “After thought” piece, I am using i++ to pass the tests, but the goal of this test is to multiply. Isn’t the ++ just adding one to my initialization value?
Honestly, I felt like I understood this piece well until I reached this lesson, please help!

Length is the number of elements in the array: there are 3 elements in the top level one, and either 2 or 3 elements in the next level of arrays.

[[1,2],[3,4],[5,6,7]]
  ^      ^      ^
  1      2      3 elements
[3, 4]
 ^  ^
 1  2 elements

The loop says “starting at a value (example here is 0), and ending when the end condition is false, increase the value by a specified amount each iteration of the loop.”

arr is length 3, so the loop keeps running while the value i is less than that.
i is 0.
i is less than 3, increase i by 1
i is 1.
i is less than 3, increase i by 1
i is 2.
i is less than 3, increase i by 1
i is 3
i is not less than 3, stop the loop.


Arrays are indexed starting at 0. So the first element is index 0, the second is index 1, third is index 2 and so on. So the loop goes from 0 to 1 to 2, and each iteration you select the element in the array at index 0 then index 1 then index 2

1 Like

.length doesn’t sum the values of an array. It returns the number of elements in the array. You need the incrementor is needed to increase i and j by one for each execution of the loop.

1 Like

Thank you for the reply. So what dancouper said is that .length basically counts the number of elements and what you’re saying is the i++ and j++ are used to continue the loop from each element to another?

so my thinking is: my initial is i = 0 so the loop starting at the first array then the length is called upon to be used for the i < arr.length formula and I have 3 elements so the loop essentially is going from [1,2] to [3,4] to [5,6,7] and stopping because no element exists beyond this loop? I hope this is the proper understanding of the length function.

And the other part I don’t understand is where the multiplication takes place. So I’m imagining that the final line of my code is performing the “for loop” by multiplying each element by each other and then the combined sums into each other if I understand right. So [i] is saying to multiply [1,2] * [3,4] * [5,6,7] and [j] is saying to multiply the inside numbers so [1 * 2] [3 * 4] [ 5 * 6 * 7] and when combined the answer comes to 5040? I just don’t understand where the product tag comes into play. I hope I’m understanding this right.

This is the way a for loop is constructed.

for(beforeFirst, doUntil, afterEach) {
    body
}

beforeFirst is code that is executed once before the first iteration of the loop.
doUntil is a condition that is checked before each iteration. If the condition is false, the loop is done.
afterEach is code that is executed after the loop body for each iteration.


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

This sets i equal to 0 once. It repeats the loop for as long as i is less than the length of arr, and it increases the value of i by 1 on each iteration.


No. That’s really not what is going on. i and j are index values.


It seems like you may have gone through earlier challenges very quickly and therefor not really absorbed the lessons. You might want to do some reviewing of both how loops work an how arrays work.

No, length is the number of elements, it’s the length of the array.

As @ArielLeslie says, I think you’ve missed a large chunk of basic stuff in the curriculum. This is very important to understand because a vast amount of what you’re asked to do builds off this knowledge. It’s going to be impossible for you to complete most things without it.

.length is a property on the array object. It also exists on other objects like strings.

You can both get and set the value.

const countMeStr = 'countMe'
console.log(countMeStr.length)
// 7

const countMeArray = [1,2,3,4,5,6,7];
console.log(countMeArray.length)
// 7

// not something you would normally do
countMeArray.length = 10;

console.log(countMeArray.length)
// 10

With the string literal there is a bit of behind the scenes “magic” going on because a literal string is not an object yet we somehow are able to get to the property (stackoverflow).

I might just have confused you more, if so I’m sorry.

1 Like