2021-06-15 07:49:18 +00:00
---
id: 56533eb9ac21ba0edf2244e1
2021-07-21 15:23:20 +00:00
title: Aninhar laços for
2021-06-15 07:49:18 +00:00
challengeType: 1
videoUrl: 'https://scrimba.com/c/cRn6GHM'
forumTopicId: 18248
dashedName: nesting-for-loops
---
# --description--
2021-07-29 16:41:44 +00:00
Se você possui um array multidimensional, você pode usar a mesma lógica no ponto de passagem anterior para iterar através de arrays e de qualquer sub-array. Exemplo:
2021-06-15 07:49:18 +00:00
```js
2021-11-03 15:22:32 +00:00
const arr = [
[1, 2], [3, 4], [5, 6]
2021-06-15 07:49:18 +00:00
];
2021-11-03 15:22:32 +00:00
for (let i = 0; i < arr.length ; i + + ) {
for (let j = 0; j < arr [ i ] . length ; j + + ) {
2021-06-15 07:49:18 +00:00
console.log(arr[i][j]);
}
}
```
2021-07-29 16:41:44 +00:00
Isso exibe no console cada subelemento dentro de `arr` , um de cada vez. Note que para o laço interno, nós estamos verificando a propriedade `.length` de `arr[i]` , desde que `arr[i]` também seja um array.
2021-06-15 07:49:18 +00:00
# --instructions--
2021-07-29 16:41:44 +00:00
Modifique a função `multiplyAll` para que retorne o produto de todos os números nos sub-arrays de `arr` .
2021-06-15 07:49:18 +00:00
# --hints--
2021-11-03 15:22:32 +00:00
`multiplyAll([[1], [2], [3]])` deve retornar `6`
2021-06-15 07:49:18 +00:00
```js
assert(multiplyAll([[1], [2], [3]]) === 6);
```
2021-11-03 15:22:32 +00:00
`multiplyAll([[1, 2], [3, 4], [5, 6, 7]])` deve retornar `5040`
2021-06-15 07:49:18 +00:00
```js
assert(
multiplyAll([
[1, 2],
[3, 4],
[5, 6, 7]
]) === 5040
);
```
2021-11-03 15:22:32 +00:00
`multiplyAll([[5, 1], [0.2, 4, 0.5], [3, 9]])` deve retornar `54`
2021-06-15 07:49:18 +00:00
```js
assert(
multiplyAll([
[5, 1],
[0.2, 4, 0.5],
[3, 9]
]) === 54
);
```
# --seed--
## --seed-contents--
```js
function multiplyAll(arr) {
2021-11-03 15:22:32 +00:00
let product = 1;
2021-06-15 07:49:18 +00:00
// Only change code below this line
// Only change code above this line
return product;
}
2021-11-03 15:22:32 +00:00
multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);
2021-06-15 07:49:18 +00:00
```
# --solutions--
```js
function multiplyAll(arr) {
2021-11-03 15:22:32 +00:00
let product = 1;
for (let i = 0; i < arr.length ; i + + ) {
for (let j = 0; j < arr [ i ] . length ; j + + ) {
2021-06-15 07:49:18 +00:00
product *= arr[i][j];
}
}
return product;
}
```