Can somebody help me with this I don’t know what is happening and am all confused i just dont know what to do!
Your code so far
function multiplyAll(arr) {
var product = 1;
// Only change code below this line
// 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36.
The goal is to multiply the product variable (given as 1) by all of the input numbers:
multiplyAll([[1,2],[3,4],[5,6,7]]); should return in 5040 ( 1 * (1*2*3*4*5*6*7) )
Your input is an Array of arrays, and the challenge is working through all inner arrays one by one to get the next multiplier. The test is named “Nesting For Loops” so hopefully that gives you a hint
I understood a bit but my confusion is what the loops are doing
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]); } }
what are these lines of code doing exactly?
Is the first For loop for (var i=0; i < arr.length; i++) adding 1,2,3 to i? like i = 3 ? Arrays start from 0 right? im really not getting it
Correct; it comes from the main function: function multiplyAll(arr) {...
And this example has 3 items like you said.
Remember that you have 2 loops.
arr[i] is the first loop… i = 0, then i = 1 … etc…
say arr=[[9,8,7,6,5,4,3],[19,18,17,16,15,14],[29,28,27,26,25]]
and i=0 (first pass)
what is arr[i].length ?
and then where does the j come from? And how does that relate back to arr[i] ?
You are getting close, but remember the challenge is nesting the loops, so arr[0][1] = 0 , arr[0][1] = 8 … and you have to go through all of the numbers (that’s why we use the loops the computer does it for us )
You are trying to grab each number inside and use them to multiply together.
The challenge is multiply the product variable (given as 1) by each number in the sub-arrays of arr
It is confusing at first (and always) but just keep at it
If you are more of a visual learner, copy the code below that we made into the link. It will step through each process of the code and you can see how the j loops through i and hopefully that will help
var arr = [
[9,8,7,6,5,4,3],[19,18,17,16,15,14],[29,28,27,26,25]
];
for (var i=0; i < arr.length; i++) {
for (var j=0; j < arr[i].length; j++) {
console.log(arr[i][j]);
}
}
I used the Visual learner then found out that it loops till it outputs all the numbers in the array then it goes up to i and changes its position from i = 0 to i = 1 so far I understand this but I’m still confused about the code I will keep trying till I get the answer i need thanks for your help!
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
multiplyAll([[1,2],[3,4],[5,6,7]]);
first run when i = 0 and j = 0 it will select 1 from [1,2]
it will do the same till the end of the length of the array
and then the loop will break
product = product *= arr[i][j] ; will multiply the array element with the product which is 1
1 * 1 = 1 is arr[0][0] targeting first 0 will look at [1,2] and then second 0 will inside [1,2] and select 1
and the selected 1 is multiplied by the product which is 1 after that the result of the first loop will be added to the product 1 * 1 = 1 , 1 * 2=2,2 * 3 = 6 on and on till the end of the arr and the final result will be 5040
A couple of things. product *= arr[i][j];
Already puts the value back into product so another product = is not needed;
The above is the same as: product = product * arr[i][j];
so your code is doing, essentially: product = product = product * arr[i][j];
I find for starting out it might be easier to visualize if you use another
variable for the subArray (or inner Array) and loop then you don’t have
the initially confusing arr[i][j]
for (var i = 0; i < arr.length; i++) {
let subArray = arr[i];
for (var j = 0; j < subArray.length; j++) {
product = product * subArray[j];
}
}