Skip to content

Commit 2039fcf

Browse files
committed
delete 別解's and fixed array-max answer
1 parent 9857a6c commit 2039fcf

File tree

4 files changed

+18
-62
lines changed

4 files changed

+18
-62
lines changed

docs/1-trial-session/10-array/_samples/array-max-reduce/index.html

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

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

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

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ function arrayMax(array) {
77
return maxValue;
88
}
99

10-
const array1 = [5986, 7202, 9347, 3593, 8166, 662, 2235, 9323, 2240, 943];
11-
const array2 = [-878, -40, -324, -410, -592, -610, -880, -65, -423, -32];
10+
const array1 = [3, 6, 8, 5, 0];
11+
const array2 = [-8, -7, -3, -1, -5];
12+
const array3 = [5986, 7202, 9347, 3593, 8166, 662, 2235, 9323, 2240, 943];
13+
const array4 = [-878, -40, -324, -410, -592, -610, -880, -65, -423, -32];
1214

1315
document.write(`<p>配列 [${array1}] の最大値は${arrayMax(array1)} です。</p>`);
1416
document.write(`<p>配列 [${array2}] の最大値は${arrayMax(array2)} です。</p/>`);
17+
document.write(`<p>配列 [${array3}] の最大値は${arrayMax(array3)} です。</p>`);
18+
document.write(`<p>配列 [${array4}] の最大値は${arrayMax(array4)} です。</p>`);
1519
document.write(`<p>空の配列の最大値は ${arrayMax([])} です。</p/>`);

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

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -134,22 +134,7 @@ document.write(`sum of array1: ${sumArray(array1)} <br>`);
134134
document.write(`sum of array2: ${sumArray(array2)} <br>`);
135135
```
136136

137-
<ViewSource url={import.meta.url} path="_samples/array-printer" />
138-
139-
### 別解
140-
141-
[`配列.join` メソッド(関数)](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/join)を使うと、次のような書き方もできます。
142-
143-
```javascript
144-
const array = ["田中", "佐藤", "鈴木"];
145-
146-
const text = array.join("");
147-
document.write(text);
148-
```
149-
150-
<ViewSource url={import.meta.url} path="_samples/array-printer-join" />
151-
152-
</Answer>
137+
<ViewSource url={import.meta.url} path="_samples/array-sum" />
153138

154139
## 中級課題
155140

@@ -183,6 +168,11 @@ const array4 = [-878, -40, -324, -410, -592, -610, -880, -65, -423, -32];
183168
配列の最初の値を初期値に設定することで解消します。
184169

185170
```javascript
171+
const array1 = [3, 6, 8, 5, 0];
172+
const array2 = [-8, -7, -3, -1, -5];
173+
const array3 = [5986, 7202, 9347, 3593, 8166, 662, 2235, 9323, 2240, 943];
174+
const array4 = [-878, -40, -324, -410, -592, -610, -880, -65, -423, -32];
175+
186176
function findMax(array) {
187177
if (array.length === 0) return; // 空配列の例外処理
188178
let maxValue = array[0];
@@ -191,6 +181,12 @@ function findMax(array) {
191181
}
192182
return maxValue;
193183
}
184+
185+
document.write(`<p>配列 [${array1}] の最大値は${arrayMax(array1)} です。</p>`);
186+
document.write(`<p>配列 [${array2}] の最大値は${arrayMax(array2)} です。</p/>`);
187+
document.write(`<p>配列 [${array3}] の最大値は${arrayMax(array3)} です。</p>`);
188+
document.write(`<p>配列 [${array4}] の最大値は${arrayMax(array4)} です。</p>`);
189+
document.write(`<p>空の配列の最大値は ${arrayMax([])} です。</p/>`);
194190
```
195191

196192
:::danger
@@ -199,25 +195,6 @@ function findMax(array) {
199195

200196
<ViewSource url={import.meta.url} path="_samples/array-max" />
201197

202-
:::info 別解 (参考)
203-
204-
[`array.reduce` メソッド](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) を使ってこのように書くこともできます。
205-
206-
```javascript
207-
function max(a, b) {
208-
if (a > b) return a;
209-
else return b;
210-
}
211-
212-
function findMax(array) {
213-
if (array.length == 0) return; //空配列をエスケープ
214-
return array.reduce(max, array[0]);
215-
}
216-
```
217-
218-
<ViewSource url={import.meta.url} path="_samples/array-max-reduce" />
219-
220-
:::
221198
</Answer>
222199

223200
<!-- オブジェクトはまだ扱っていないためコメントアウト

0 commit comments

Comments
 (0)