Nested for loop multiply

*Tell us what’s happening:
I am having trouble understanding the nested loop part. So once the top loop is evaluated, the second loop executes until its condition is met right and it outputs the array indexes as well with [i]?

Your code so far


function multiplyAll(arr) {
var product = 1;
// Only change code below this line

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

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

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

Your browser information:

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

Challenge: Nesting For Loops

Link to the challenge:

hi @codeLive ,

my anology for this array is like book

var book = [['this', 'just'] , ['myown', 'array', 'analogy']]; 

and if you read it like book has length of 2 chapters.
in chapter 1 has ['title1', 'title2'] length of 2 titles.
and in chapter 2 has ['title1', 'title2', 'title3'] length of 3 titles.

so in first loop i would get chapter[1] and chapter[2].

and the inner loop i get chapter[1][title1] which is title “this”, and
chapter[1][title2] which title “just”.
and loop will end with the chapter[2][title3] which title “analogy”.

hope it helps

edit: javascript count index length from 0.

I see what your saying so correct me if I am wrong but the first loop measures the length of the books array which is two chapters (elements) . After the first loop is true, the inner loop runs two times getting the indexes of the chapters? @sobadrdb

yes, like that

but the inner loop runs how many index length of titles, when length titles false, loop jump back to second first loop of chapter index, then re enter the inner loop of titels index length of chapter second.

1 Like

Maybe this will help ?

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+"] = [" + arr[i] +"] arr["+i+"]["+j+"] = "+ arr[i][j]);
	}
}
1 Like

@Da_vey Can you go into more detail about what your trying to explain ?