freeCodeCamp/curriculum/challenges/portuguese/02-javascript-algorithms-an.../basic-javascript/nesting-for-loops.md

93 lines
1.7 KiB
Markdown
Raw Normal View History

---
id: 56533eb9ac21ba0edf2244e1
title: Aninhar laços for
challengeType: 1
videoUrl: 'https://scrimba.com/c/cRn6GHM'
forumTopicId: 18248
dashedName: nesting-for-loops
---
# --description--
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:
```js
const arr = [
[1, 2], [3, 4], [5, 6]
];
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
console.log(arr[i][j]);
}
}
```
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.
# --instructions--
Modifique a função `multiplyAll` para que retorne o produto de todos os números nos sub-arrays de `arr`.
# --hints--
`multiplyAll([[1], [2], [3]])` deve retornar `6`
```js
assert(multiplyAll([[1], [2], [3]]) === 6);
```
`multiplyAll([[1, 2], [3, 4], [5, 6, 7]])` deve retornar `5040`
```js
assert(
multiplyAll([
[1, 2],
[3, 4],
[5, 6, 7]
]) === 5040
);
```
`multiplyAll([[5, 1], [0.2, 4, 0.5], [3, 9]])` deve retornar `54`
```js
assert(
multiplyAll([
[5, 1],
[0.2, 4, 0.5],
[3, 9]
]) === 54
);
```
# --seed--
## --seed-contents--
```js
function multiplyAll(arr) {
let product = 1;
// Only change code below this line
// Only change code above this line
return product;
}
multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);
```
# --solutions--
```js
function multiplyAll(arr) {
let product = 1;
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
product *= arr[i][j];
}
}
return product;
}
```