-
Notifications
You must be signed in to change notification settings - Fork 3
配列の章の演習問題の修正 #465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
配列の章の演習問題の修正 #465
Changes from all commits
d8618c9
aa54eee
ca33b54
0de653b
897b6d2
c265778
c896ee1
5660c71
06e129d
585bae8
2eaa68b
2e050d1
2de995b
94f4d76
08dea05
6faba10
c9f5f1d
6d6ce8b
aa1f803
1cc3fb3
43b5770
51177ed
724d87e
bd69739
f5a2f2a
f636389
1bef64e
352ddaa
9437934
9d9ae44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -124,11 +124,16 @@ increment(); | |
|
|
||
| ::: | ||
|
|
||
| ## モジュール化 | ||
| ## パーツに分割する | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> と呼びます。 | ||
| パーツに分割すると、次のようなメリットがあります。 | ||
|
|
||
| モジュール化前: | ||
| - ブロックあたりのコードが短くなるので、読みやすい | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コードを短くできるというのは、少し違う気がするかも。単純に関心を分離できることが重要で、コードを短くすることが大切なわけではない気がしてる。コードは短いのがよいということはないはず。 |
||
| - パーツごとにテストができるので、デバッグがしやすい | ||
| - 汎用性のあるパーツなら、使いまわしができる | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 汎用性のないものはそもそもあるのかなあ。汎用性のないものを想定させると、ただ無関係のものを凝集させただけのコードを書くのを助長しそう。 |
||
|
|
||
| 以下の例では、階段を表示する操作の中の、文字列を繰り返す操作を `repeat` 関数というパーツに分けています。 | ||
|
|
||
| ```javascript | ||
| const stringToRepeat = "☆"; | ||
|
|
@@ -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>ものを返します。 | ||
| ::: | ||
|
|
||
| --- | ||
|
|
||
| ## 基礎演習 | ||
| ## 基礎課題 | ||
|
|
||
| ### 最大値 | ||
|
|
||
|
|
@@ -208,7 +208,7 @@ function max(a, b) { | |
|
|
||
| </Answer> | ||
|
|
||
| ## 中級演習 | ||
| ## 中級課題 | ||
|
|
||
| ### 携帯電話料金 | ||
|
|
||
|
|
||
| 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} です。`); |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ import Answer from "@site/src/components/Answer"; | |
|
|
||
| ## 配列 | ||
|
|
||
| JavaScript における配列は、複数の値を並べて一つにまとめたオブジェクトです。`[` から `]` で囲まれた部分は配列を生成する式になります。 | ||
| JavaScript における配列は、複数の値を並べて一つにまとめた値です。`[` から `]` で囲まれた部分は配列を生成する式になります。 | ||
|
|
||
| ```javascript | ||
| const studentNames = ["田中", "佐藤", "鈴木"]; | ||
|
|
@@ -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]; | ||
|
|
@@ -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である空の配列を除外することを忘れないでください! | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. このエクスクラメーションマークどうでしょう? |
||
| ::: | ||
|
|
||
| <ViewSource url={import.meta.url} path="_samples/array-max" /> | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.