Nested arrays inside of functions* Multiplying back on product- MindBlown

Tell us what’s happening:
Describe your issue in detail here.

``````  **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]]);
``````
``````  **Your browser information:**
``````

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

Challenge: Nesting For Loops

Well, right now that isn’t doing anything.

Do you have code that you’ve tried?

You can take a look at the description and they have an example using a nested loop - that’s a good place to start. There is more than one way to solve this, but that is definitely one way.

Thanks Kevin, I have a major brainDiskError every time I come across something new. I watched the video and read through the comments then wrote it out a few times. Makes sense now. Until I see it used in a slightly different way. My question is with regards to how the multiplication itself works.

Could you explain how we got to the number 5040? I’ve tried doing the number myself using a calculator but can’t get to the answer.

Join the club. Yeah, this is hard stuff, don’t be too hard on yourself.

Could you explain how we got to the number 5040?

For this input:

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

First of all, remember that multiplication (like addition) is commutative - it doesn’t matter what order it is, you can scramble it up and will always get the same result. In other words, we will get the same answer no matter what order we multiply them.

So, this is: `1 X 2 X 3 X 4 X 5 X 6 X 7`. If you type that into a calculator, it is 5040. If you remember a little math, you’ll also know that this is the same as `7!` - same result, 5040.

So, this is a pretty simple math problem. Perhaps you either mistyped something into the calculator. Or perhaps you misunderstood what is being done.

Does that clear things up or have I misunderstood the question?

No I am the one misunderstanding the question which is making it difficult to relay what I’m trying to understand if that makes sense.

let me try break down my thinking so I can explain to you what it is i am struggling with.

``````var arr = [
for (var i=0; i < arr.length; i++)
``````

so this would loop through the first array returning values of? 0 ,1, 2 as the arr.length is 3?

Then for the second part the value outputs of J would be what?

Then how would those multiply together. Sorry this is not making much sense. I feel highly incompetents right now just trying to explain this. Gonna go over it a few more times. I’m reading the comments but it’s just not clicking at all.

Hi @CodeLooper !

Nested arrays can be confusing. But let’s break down the multiplication like this. Remember that product will keep getting updated with the new value.

``````arr = [[1,2],[3,4],[5,6,7]]

var product = 1

product = 1(product) * 1(arr[0][0])

product = 1

product = 1(product) * 2(arr[0][1])

product = 2

product = 2(product) * 3(arr[1][0])

product = 6

product = 6(product) * 4(arr[1][1])

product = 24

product = 24(product) * 5(arr[2][0])

product = 120

product = 120(product) * 6(arr[2][1])

product = 720

product = 720(product) * 7(arr[2][2])

product = 5040
``````

Hope that visual helps!

1 Like

The outer loop will loop through these three arrays.

``````arr[0] = [1,2]
arr[1] = [3,4]
arr[2] = [5,6,7]
``````

J represents each element in the nested array

`arr = [[1,2],[3,4],[5,6,7]]`

``````//if i=0 j=0
arr[i][j] = 1

// when i=0 j=1
arr[i][j] = 2

// when i=1 j=0
arr[i][j]  = 3
``````

Hope that makes sense!

1 Like

Wow, thank you very much! This makes absolute sense to me now. I have actually solved this puzzle before in a far more basic way on the Google Grasshopper app. Made total sense then, absolutely blew my mind now. This visual representation really makes it easy to understand the logic behind this. I don’t know why but my brain was trying to multiply the values within the different arrays to get to some answer. Meanwhile we’re just looping through two arrays [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] taking those values then multiplying them onto the product variable. Tada. Sorry for my poor explanation , i have only been doing JS for ±100hours now. Mostly Udemy lectures and examples, which are great for intro but then I find the same question in a slightly different variant and my mind is blown.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.