2021-05-05 10:13:49 -07:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244e1
|
|
|
|
title: 循環嵌套
|
|
|
|
challengeType: 1
|
|
|
|
videoUrl: 'https://scrimba.com/c/cRn6GHM'
|
|
|
|
forumTopicId: 18248
|
|
|
|
dashedName: nesting-for-loops
|
|
|
|
---
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
|
|
|
如果你有一個二維數組,可以使用相同的邏輯,先遍歷外面的數組,再遍歷裏面的子數組。 下面是一個例子:
|
|
|
|
|
|
|
|
```js
|
2021-11-06 08:56:52 -07:00
|
|
|
const arr = [
|
|
|
|
[1, 2], [3, 4], [5, 6]
|
2021-05-05 10:13:49 -07:00
|
|
|
];
|
2021-11-06 08:56:52 -07:00
|
|
|
|
|
|
|
for (let i = 0; i < arr.length; i++) {
|
|
|
|
for (let j = 0; j < arr[i].length; j++) {
|
2021-05-05 10:13:49 -07:00
|
|
|
console.log(arr[i][j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
這裏一次輸出了 `arr` 中的每個子元素。 提示,對於內部循環,我們可以通過 `arr[i]` 的 `.length` 來獲得子數組的長度,因爲 `arr[i]` 本身就是一個數組。
|
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
|
|
|
修改函數 `multiplyAll`,獲得 `arr` 內部數組的每個數字相乘的結果 product。
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
2021-11-06 08:56:52 -07:00
|
|
|
`multiplyAll([[1], [2], [3]])` 應該返回 `6`
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(multiplyAll([[1], [2], [3]]) === 6);
|
|
|
|
```
|
|
|
|
|
2021-11-06 08:56:52 -07:00
|
|
|
`multiplyAll([[1, 2], [3, 4], [5, 6, 7]])` 應該返回 `5040`
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
multiplyAll([
|
|
|
|
[1, 2],
|
|
|
|
[3, 4],
|
|
|
|
[5, 6, 7]
|
|
|
|
]) === 5040
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
2021-11-06 08:56:52 -07:00
|
|
|
`multiplyAll([[5, 1], [0.2, 4, 0.5], [3, 9]])` 應該返回 `54`
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
multiplyAll([
|
|
|
|
[5, 1],
|
|
|
|
[0.2, 4, 0.5],
|
|
|
|
[3, 9]
|
|
|
|
]) === 54
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function multiplyAll(arr) {
|
2021-11-06 08:56:52 -07:00
|
|
|
let product = 1;
|
2021-05-05 10:13:49 -07:00
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
// Only change code above this line
|
|
|
|
return product;
|
|
|
|
}
|
|
|
|
|
2021-11-06 08:56:52 -07:00
|
|
|
multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);
|
2021-05-05 10:13:49 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function multiplyAll(arr) {
|
2021-11-06 08:56:52 -07:00
|
|
|
let product = 1;
|
|
|
|
for (let i = 0; i < arr.length; i++) {
|
|
|
|
for (let j = 0; j < arr[i].length; j++) {
|
2021-05-05 10:13:49 -07:00
|
|
|
product *= arr[i][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return product;
|
|
|
|
}
|
|
|
|
```
|