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
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).
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)
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).