Skip to content

Commit 9857a6c

Browse files
committed
Changed Basic Exercise in 1/functions
1 parent 4190c74 commit 9857a6c

File tree

6 files changed

+27
-25
lines changed

6 files changed

+27
-25
lines changed

docs/1-trial-session/10-array/_samples/array-printer-join/index.html

Lines changed: 0 additions & 10 deletions
This file was deleted.

docs/1-trial-session/10-array/_samples/array-printer-join/script.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

docs/1-trial-session/10-array/_samples/array-printer/script.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

docs/1-trial-session/10-array/_samples/array-printer/index.html renamed to docs/1-trial-session/10-array/_samples/array-sum/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html lang="ja">
33
<head>
44
<meta charset="ja" />
5-
<title>配列表示関数 解答例</title>
5+
<title>配列の和 解答例</title>
66
</head>
77
<body>
88
<script src="./script.js"></script>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
2+
const array2 = [-3, -1, 9, -10, 3, 7, 6, 1, 0, 5];
3+
4+
function sumArray(array) {
5+
let sum = 0;
6+
for (let i = 0; i < array.length; i += 1) {
7+
sum += array[i];
8+
}
9+
return sum;
10+
}
11+
12+
document.write(`sum of array1: ${sumArray(array1)} <br>`);
13+
document.write(`sum of array2: ${sumArray(array2)} <br>`);

docs/1-trial-session/10-array/index.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,28 @@ document.write(studentNames); // 田中,佐藤,鈴木,内藤
110110

111111
### 連続表示
112112

113-
- 引数に与えられた配列の要素を、通常のfor文を使って順番に表示してみましょう
113+
- 引数に与えられた配列の、要素の和を取る関数 `sumArray` を書いてみましょう
114114

115115
:::tip
116-
変数 `i``0` から `(作成した配列の長さ) - 1` まで順番に増やしながら、配列の `i` 番目の要素を表示しましょう
116+
変数 `i``0` から `(作成した配列の長さ) - 1` まで順番に増やしながら、配列の `i` 番目の要素を足してみましょう
117117
:::
118118

119119
<Answer>
120120

121121
```javascript
122-
const array = ["田中", "佐藤", "鈴木"];
122+
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
123+
const array2 = [-3, -1, 9, -10, 3, 7, 6, 1, 0, 5];
123124

124-
for (let i = 0; i < array.length; i += 1) {
125-
document.write(array[i]);
125+
function sumArray(array) {
126+
let sum = 0;
127+
for (let i = 0; i < array.length; i += 1) {
128+
sum += array[i];
129+
}
130+
return sum;
126131
}
132+
133+
document.write(`sum of array1: ${sumArray(array1)} <br>`);
134+
document.write(`sum of array2: ${sumArray(array2)} <br>`);
127135
```
128136

129137
<ViewSource url={import.meta.url} path="_samples/array-printer" />

0 commit comments

Comments
 (0)