function multiplyAll(arr) {
var product = 1;
// Only change code below this line
for (var x=0;x<arr.length;x++){
for (var y=0;y<arr[x].length;y++){
product=[x]*[y];
}
}
// Only change code above this line
return product;
}
// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15.
think about this though, x is getting you part of the array and y is getting you another part of it. you want to multiply each part of the array with the variable product.
I will tell you what the JavaScript interpreter thinks you wrote.
Starting on the right side of the assignment operator, you are multiplying an array with one element who’s value is x by an array with one element who’s value is y. Then you are assigning that value to product.
Because you are replacing the value of product in each iteration of the inner for loop with this, only the last inner for loop iteration will determine the final value of product.
First, think about what the values of x and y are on any given iteration. The are index values of arrays and NOT the actual values of the arrays.
Second, you do not want to replace the value of product with just the product of two number. Instead you want to multiply the current value of product by the current subarray element AND THEN assign the result back to product. To refer to the inner subarray, you would write arr[x][y].
so just to clarify how javascript interpret the code,
x will be the 1st number and goes to 2nd loop y will be 2nd number and if the array ends it goes back to x in the next array. BUT if the array has 3 number it will be x,y,y??
think of it this way,
x and y are two guys going through baskets together.
x is in charge of the outer array, the three big baskets, each time he picks up a basket he gives it to y.
y then looks at each item inside the basket…
and assuming you have your multiplier set correctly, multiplies each item by the number contained in product and makes the product of that multiplication the new value of the variable product.
Each time y finishes a basket the focus switches back to x. in this way every number ends up being counted.
so when we type arr[x][y] within those for loops, what we are really saying is the basket that x has most recently picked up and the item within that basket that y is currently looking at.