# Why does this work the way it does?

I am taking the course and I learn new things besides of what is written in the course.

Some of the things I see / learn I wonder how common would it be? and if they are like seen in a very particular case, or if they are more common?

Take this example:

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

No, I am not taking about the nested array. More specifically I am talking about this line:

`````` product = product * arr[i][j];
``````

There was no way in the world I was gonna get to the `arr[i] [j];` obviously I donâ€™ remember seen a code written that way before, and WHY would it work that way?

I ask WHY would this work this way because it seems very specific.

What if you want to do a math like:

`([1 +2]+[3*4]-[5+6-7])`

^^ A variation of adding, substracting, multiplication, division, I would think might be more common than straight multiplication for everything.

The `arr[i] [j];` looks like a pretty neat trick. I just have to remember that using a functionâ€™s argument and adding the [i][j] from the nested array will multiply everything.

Is there a trick to include a shortcut for a mix of +, - , *, /, within the formula as well?

I donâ€™t understand this line:

There was no way in the world I was gonna get to the `arr[i] [j];`

Are you talking about adding a space? Go ahead. JS doesnâ€™t usually care about whitespace. But most people wonâ€™t and most linters would probably complain.

What if you want to do a math like:

`([1 +2]+[3*4]-[5+6-7])`

^^ A variation of adding, substracting, multiplication, division, I would think might be more common than straight multiplication for everything.

Is that â€śmath mathâ€ť? Then I donâ€™t know what the brackets mean. Is that â€śJS mathâ€ť? Then you are creating arrays and trying to add and subtract? What would that mean?

The `arr[i] [j];` looks like a pretty neat trick. I just have to remember that using a functionâ€™s argument and adding the [i][j] from the nested array will multiply everything.

This does not multiply anything. This accesses a piece of data.

``````const arr [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]

arr[0] // [1, 2, 3]

arr[0][1] // 2

arr[1][2] // 6

arr[2][0] // 7
``````

There is multiplication happening above, just accessing elements in the array, or the arrays inside the array.

In the code:

``````product = product * arr[i][j];
``````

The multiplication is happening because of `product = product *`, not because of `arr[i][j]` - that is just feeding a value to the multiplication.

1 Like

Yes, the ` product = product *,` creates internal multiplication within the `arr[i][j] `

Yes, I mean, what if I want to do actual math mathâ€¦

`((1 +2)+(3*4)-(5+6-7))`

I suppose I can find a long way to do by creating a bunch of variables and then creating a function that looks just like the math formula.

It was interesting that JS multiplies everything within the array when doing something like this:

``````product * arr[i][j];
``````

Yes, the `product = product *,` creates internal multiplication within the `arr[i][j]`

This does not "create internal multiplication within the `arr[i][j]`". That line multiplies product by a single value in the array, depending on the value of i and j. It is the looping and changing of those variables that is doing the work of hitting all of the values in the array.

Yes, I mean, what if I want to do actual math mathâ€¦

`((1 +2)+(3*4)-(5+6-7))`

I suppose I can find a long way to do by creating a bunch of variables and then creating a function that looks just like the math formula.

OK, I think I see what youâ€™re asking. How would you do more complex calculations on an array (or array of arrays) of data?

That is a hard question to answer because we donâ€™t have enough information. Would it always be the first value added to the second, the third multiplied to the fourth, etc.? Would it depend on the size?

Clearly youâ€™d need a more complicated function. You may not be able to use a loop. Maybe you could with some lookup table of actions, but grouping gets weird. Youâ€™d have to have some way to tell the function what to do. At that point, I would assume that the different values in the array have different meanings - at that point I would argue that an array isnâ€™t the best data structure, maybe an object would make more sense.

Is there a trick to include a shortcut for a mix of +, - , *, /, within the formula as well?

Shortcut? No. Is it possible? Sure. Is it worth the complexity to keep this in a loop if that is what you need? Probably not.

But again, itâ€™s very hard to say without knowing the use case.

2 Likes

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