Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d8618c9
基礎課題の修正 (for ofメインに)
aster-void Oct 14, 2023
aa54eee
例外処理 -> 除外
aster-void Oct 14, 2023
ca33b54
中級課題をfor ofに修正
aster-void Oct 14, 2023
0de653b
最終調整
aster-void Oct 14, 2023
897b6d2
課題のサブタイトルの表示方法の修正
aster-void Oct 14, 2023
c265778
ダサかったのでやりなおし
aster-void Oct 14, 2023
c896ee1
rename 課題 -> 演習
aster-void Oct 14, 2023
5660c71
rename 演習 -> 課題
aster-void Oct 14, 2023
06e129d
fix
aster-void Oct 14, 2023
585bae8
delete mysterious ```
aster-void Oct 15, 2023
2eaa68b
renamed arrays and elems to "numbers" and "number"
aster-void Oct 15, 2023
2e050d1
Merge master and fix confi's
aster-void Oct 15, 2023
2de995b
なぜかfindMaxが戻ってたので再度置換
aster-void Oct 15, 2023
94f4d76
Merge pull request #467 from ut-code/rename-exercise-to-assignment
aster-void Oct 15, 2023
08dea05
fixed merging issue
aster-void Oct 16, 2023
6faba10
解答例を日本語に
aster-void Oct 16, 2023
c9f5f1d
解答例を日本語に
aster-void Oct 16, 2023
6d6ce8b
日本語に
aster-void Oct 16, 2023
aa1f803
for of -> for 〜 of
aster-void Oct 16, 2023
1cc3fb3
テキストが消えてた (なぜ?) ので修正
aster-void Oct 16, 2023
43b5770
改悪されてたので元通りに
aster-void Oct 16, 2023
51177ed
value -> number
aster-void Oct 16, 2023
724d87e
pタグが壊れてたので修正
aster-void Oct 16, 2023
bd69739
pタグ修正
aster-void Oct 16, 2023
f5a2f2a
基礎課題の解答例修正
aster-void Oct 18, 2023
f636389
モジュール化の説明
aster-void Oct 18, 2023
1bef64e
配列は値 (すでに修正してる気がする...)
aster-void Oct 18, 2023
352ddaa
findMax -> findMaxNumber
aster-void Oct 18, 2023
9437934
Merge branch '462-fix-array-exercise' of github.com:ut-code/utcode-le…
aster-void Oct 18, 2023
9d9ae44
配列の合計値は: -> 配列の合計値は
aster-void Oct 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/1-trial-session/08-loop/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ const string2 = `10から2を引くと${10 - 2}です。`;

---

## 基礎演習
## 基礎課題

### 1 ~ 10 の積

Expand Down Expand Up @@ -221,7 +221,7 @@ document.write(product);

</Answer>

## 中級演習
## 中級課題
Comment thread
aster-void marked this conversation as resolved.

### 素数判定問題

Expand Down
26 changes: 13 additions & 13 deletions docs/1-trial-session/09-functions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,16 @@ increment();

:::

## モジュール化
## パーツに分割する
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここを変える必要はない気がする?


複雑な操作を <Term type="javascriptFunction">関数</Term> として <Term strong type="javascriptModularization">モジュール化</Term> して複数のブロックに分解することで、コードの可読性を上げることができます。
複雑な操作を複数の <Term type="javascriptFunction">関数</Term> ブロックに分解することで、コードの可読性を上げることができます。この操作を <Term strong type="javascriptModularization">モジュール化</Term> と呼びます。
パーツに分割すると、次のようなメリットがあります。

モジュール化前:
- ブロックあたりのコードが短くなるので、読みやすい
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コードを短くできるというのは、少し違う気がするかも。単純に関心を分離できることが重要で、コードを短くすることが大切なわけではない気がしてる。コードは短いのがよいということはないはず。

- パーツごとにテストができるので、デバッグがしやすい
- 汎用性のあるパーツなら、使いまわしができる
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

汎用性のないものはそもそもあるのかなあ。汎用性のないものを想定させると、ただ無関係のものを凝集させただけのコードを書くのを助長しそう。


以下の例では、階段を表示する操作の中の、文字列を繰り返す操作を `repeat` 関数というパーツに分けています。

```javascript
const stringToRepeat = "☆";
Expand All @@ -142,29 +147,24 @@ for (let i = 0; i < 10; i += 1) {
}
```

モジュール化後:

```javascript
function repeat(stringToRepeat, times) {
function repeat(stringToRepeat, count) {
let result = "";
for (let j = 0; j < times; j += 1) {
for (let j = 0; j < count; j += 1) {
result += stringToRepeat;
}
return result;
}

for (let i = 0; i < 10; i += 1) {
document.write(repeat("☆", i));
document.write("<br>");
}
```

:::note
この例における`repeat`<Term type="javascriptFunction">関数</Term>は、第一<Term type="javascriptParameter">引数</Term>の<Term type="javascriptString">文字列</Term>を第二<Term type="javascriptParameter">引数</Term>回だけ繰り返し<Term type="javascriptStringConcatenation">足した</Term>ものを返します。
:::

---

## 基礎演習
## 基礎課題

### 最大値

Expand Down Expand Up @@ -208,7 +208,7 @@ function max(a, b) {

</Answer>

## 中級演習
## 中級課題

### 携帯電話料金

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>findMax関数 解答例 (reduce)</title>
<title>findMaxNumber関数 解答例 (reduce)</title>
</head>
<body>
<script src="./script.js"></script>
Expand Down
32 changes: 21 additions & 11 deletions docs/1-trial-session/10-array/_samples/array-max/script.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
function findMax(array) {
if (array.length == 0) return; //空配列の例外処理
let maxValue = array[0];
for (let i = 0; i < array.length; i += 1) {
if (array[i] > maxValue) maxValue = array[i];
function findMaxNumber(numbers) {
if (numbers.length === 0) return; // 空配列を除外
let maxNumber = numbers[0];
for (const number of numbers) {
if (maxNumber < number) {
maxNumber = number;
}
}
return maxValue;
return maxNumber;
}

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

document.write(`<p>配列 [${array1}] の最大値は${findMax(array1)} です。</p>`);
document.write(`<p>配列 [${array2}] の最大値は${findMax(array2)} です。</p/>`);
document.write(`<p>配列 [${array3}] の最大値は${findMax(array3)} です。</p>`);
document.write(`<p>配列 [${array4}] の最大値は${findMax(array4)} です。</p>`);
document.write(`<p>空の配列の最大値は ${findMax([])} です。</p/>`);
document.write(
`<p>配列 [${array1}] の最大値は${findMaxNumber(array1)} です。</p>`,
);
document.write(
`<p>配列 [${array2}] の最大値は${findMaxNumber(array2)} です。</p>`,
);
document.write(
`<p>配列 [${array3}] の最大値は${findMaxNumber(array3)} です。</p>`,
);
document.write(
`<p>配列 [${array4}] の最大値は${findMaxNumber(array4)} です。</p>`,
);
document.write(`<p>空の配列の最大値は ${findMaxNumber([])} です。</p>`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

let sum = 0;
for (const number of numbers) {
sum += number;
}

document.write(`配列の合計値は ${sum} です。`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>配列の和 解答例</title>
</head>
<body>
<script src="./script.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

let sum = 0;
for (let i = 0; i < numbers.length; i += 1) {
sum += numbers[i];
}

document.write(`配列の合計値は ${sum} です。`);
13 changes: 0 additions & 13 deletions docs/1-trial-session/10-array/_samples/array-sum/script.js

This file was deleted.

95 changes: 65 additions & 30 deletions docs/1-trial-session/10-array/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Answer from "@site/src/components/Answer";

## 配列

JavaScript における配列は、複数の値を並べて一つにまとめたオブジェクトです。`[` から `]` で囲まれた部分は配列を生成する式になります。
JavaScript における配列は、複数の値を並べて一つにまとめた値です。`[` から `]` で囲まれた部分は配列を生成する式になります。

```javascript
const studentNames = ["田中", "佐藤", "鈴木"];
Expand Down Expand Up @@ -108,45 +108,70 @@ document.write(studentNames); // 田中,佐藤,鈴木,内藤

## 基礎課題

### 連続表示
### 要素の和

- 引数に与えられた配列の、要素の和を取る関数 `sumArray` を書いてみましょう
- 配列があります。その配列の要素の和を求めてみましょう

:::tip
変数 `i` を `0` から `(作成した配列の長さ) - 1` まで順番に増やしながら、配列の `i` 番目の要素を足してみましょう。
`for 〜 of` 文を使って、配列のそれぞれの要素に対して操作を実行します。
:::

:::note
次の配列をテスト用に使ってください。

```javascript
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
```

```javascript
const numbers = [-3, -1, 9, -10, 3, 7, 6, 1, 0, 5];
```

:::

<Answer>

```javascript
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const array2 = [-3, -1, 9, -10, 3, 7, 6, 1, 0, 5];
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

function sumArray(array) {
let sum = 0;
for (let i = 0; i < array.length; i += 1) {
sum += array[i];
}
return sum;
let sum = 0;
for (const number of numbers) {
sum += number;
}

document.write(`配列の合計値は ${sum} です。`);
```

<ViewSource url={import.meta.url} path="_samples/array-sum-for-of" />

### 別解

`for 〜 of` 文を使わず、次のように書くこともできます。

```javascript
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

let sum = 0;
for (let i = 0; i < numbers.length; i += 1) {
sum += numbers[i];
}

document.write(`sum of array1: ${sumArray(array1)} <br>`);
document.write(`sum of array2: ${sumArray(array2)} <br>`);
document.write(`配列の合計値は ${sum} です。`);
```

<ViewSource url={import.meta.url} path="_samples/array-sum" />
<ViewSource url={import.meta.url} path="_samples/array-sum-simple-for" />

</Answer>

## 中級課題

### 最大値
### 配列の最大値

引数にひとつの配列が与えられたとき、その配列の最大値を求める関数 `findMax` を作成しましょう。
引数に配列を一つ取り、その配列の最大値を求める関数 `findMaxNumber` を作成しましょう。

:::note

テスト用に、ランダムに生成された以下の配列を使ってよいものとします
テスト用に、ランダムに生成された次の配列を使ってください

```javascript
const array1 = [3, 6, 8, 5, 0];
Expand Down Expand Up @@ -175,24 +200,34 @@ const array2 = [-8, -7, -3, -1, -5];
const array3 = [5986, 7202, 9347, 3593, 8166, 662, 2235, 9323, 2240, 943];
const array4 = [-878, -40, -324, -410, -592, -610, -880, -65, -423, -32];

function findMax(array) {
if (array.length === 0) return; // 空配列の例外処理
let maxValue = array[0];
for (let i = 0; i < array.length; i += 1) {
if (array[i] > maxValue) maxValue = array[i];
function findMaxNumber(numbers) {
if (numbers.length === 0) return; // 空配列を除外
let maxNumber = numbers[0];
for (const number of numbers) {
if (maxNumber < number) {
maxNumber = number;
}
}
return maxValue;
return maxNumber;
}

document.write(`<p>配列 [${array1}] の最大値は${findMax(array1)} です。</p>`);
document.write(`<p>配列 [${array2}] の最大値は${findMax(array2)} です。</p/>`);
document.write(`<p>配列 [${array3}] の最大値は${findMax(array3)} です。</p>`);
document.write(`<p>配列 [${array4}] の最大値は${findMax(array4)} です。</p>`);
document.write(`<p>空の配列の最大値は ${findMax([])} です。</p/>`);
document.write(
`<p>配列 [${array1}] の最大値は${findMaxNumber(array1)} です。</p>`,
);
document.write(
`<p>配列 [${array2}] の最大値は${findMaxNumber(array2)} です。</p>`,
);
document.write(
`<p>配列 [${array3}] の最大値は${findMaxNumber(array3)} です。</p>`,
);
document.write(
`<p>配列 [${array4}] の最大値は${findMaxNumber(array4)} です。</p>`,
);
document.write(`<p>空の配列の最大値は ${findMaxNumber([])} です。</p>`);
```

:::danger
配列の長さにかかわらず配列の最初の値を使うような処理をする場合は、長さが0である空の配列を渡された時に例外処理することを忘れないでください
配列の長さにかかわらず配列の最初の値を使うような処理をする場合は、長さが0である空の配列を除外することを忘れないでください
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このエクスクラメーションマークどうでしょう?

:::

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