-
Notifications
You must be signed in to change notification settings - Fork 3
Rewritten functions #386
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
Closed
Closed
Rewritten functions #386
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
0baea27
Bandwidthは帯域幅であり、通信量ではない
aster-void 157933e
returnによる条件分岐の説明追加
aster-void 1c1d18b
モジュール化追加
aster-void 2f6682e
typo fix
aster-void e30b9d9
コメント削除
aster-void 774ca64
コメントアウト消し忘れ
aster-void f31c99e
Update definitions.js
aster-void 62d39a5
Update index.md
aster-void b4771ce
typo fix
aster-void 06dfb82
referencePage追加
aster-void 4542878
空行削除 車の運転可能年齢の修正
aster-void b4bb3e9
strongタグ=>**
aster-void 9336203
Update index.md
aster-void 107e9e1
if~elseのリンクの修正
aster-void 5d0d40c
Fixed docusaurus reference link of mudularization
aster-void 7c3e5d4
index.mdがあっても動くとは思いますが一応消。しました
aster-void 0271244
docusaurusの"バグ"対応
aster-void File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,7 +47,7 @@ greet("morning", "佐藤"); | |
|
|
||
| ## <Term type="javascriptReturnValue">戻り値</Term> | ||
|
|
||
| <p><Term type="javascriptFunction">関数</Term>呼び出しは<Term type="javascriptExpression">式</Term>の一種です。<Term type="javascriptFunction">関数</Term>定義内で <strong>return 文</strong>を用いると、<Term type="javascriptFunction">関数</Term>の実行が停止され、<Term type="javascriptFunction">関数</Term>呼び出し<Term type="javascriptExpression">式</Term>の<Term type="javascriptEvaluation">評価</Term>結果が確定します。この値を<Term strong type="javascriptReturnValue">戻り値</Term>と呼びます。ある<Term type="javascriptValue">値</Term>を<Term type="javascriptReturnValue">戻り値</Term>として設定して関数の実行を終了することを、<Term type="javascriptFunction">関数</Term>がその<Term type="javascriptValue">値</Term>を<Term strong type="javascriptReturn">返す</Term>と表現します。</p> | ||
| <p><Term type="javascriptFunction">関数</Term>呼び出しは<Term type="javascriptExpression">式</Term>の一種です。<Term type="javascriptFunction">関数</Term>定義内で <strong>return 文</strong> を用いると、<Term type="javascriptFunction">関数</Term>の実行が停止され、<Term type="javascriptFunction">関数</Term>呼び出し<Term type="javascriptExpression">式</Term>の<Term type="javascriptEvaluation">評価</Term>結果が確定します。この値を<Term strong type="javascriptReturnValue">戻り値</Term>と呼びます。ある<Term type="javascriptValue">値</Term>を<Term type="javascriptReturnValue">戻り値</Term>として設定して関数の実行を終了することを、<Term type="javascriptFunction">関数</Term>がその<Term type="javascriptValue">値</Term>を<Term strong type="javascriptReturn">返す</Term>と表現します。</p> | ||
|
|
||
| ```javascript | ||
| function add(a, b) { | ||
|
|
@@ -61,6 +61,22 @@ document.write(add(3, 4)); | |
|
|
||
| <video src={returnValueVideo} controls autoPlay muted loop /> | ||
|
|
||
| :::tip **return文**が実行された時点で関数が終了するため、次のように書くことで [if ~ else 文](../07-if-statement/#if--else-if--else)や [|| (OR) 演算子](../06-boolean/#%E8%AB%96%E7%90%86%E6%BC%94%E7%AE%97%E5%AD%90)の繰り返しを避けつつ、複数の条件のついた処理を実行することができます。 | ||
|
|
||
| ```javascript | ||
| let age = 21; | ||
| let hasDriverLicense = true; | ||
| let isDrunk = true; | ||
| function tryToDrive() { | ||
| // if文の実行する式が一行だけの場合、{}を省略できます。 | ||
| if (age < 18) return; | ||
| if (hasDriverLicense === false) return; | ||
| if (isDrunk) return; | ||
| document.write("車を運転できます。"); | ||
| } | ||
| ``` | ||
| ::: | ||
|
|
||
| ## <Term type="javascriptVariable">変数</Term>の<Term type="javascriptScope">スコープ</Term> | ||
|
|
||
| <p><Term type="javascriptFunction">関数</Term>内で<Term type="javascriptDeclaration">宣言</Term>された<Term type="javascriptVariable">変数</Term>は、<Term type="javascriptFunction">関数</Term>内でのみ有効です。<Term type="javascriptVariable">変数</Term>が有効な範囲のことを、その<Term type="javascriptVariable">変数</Term>の<Term type="javascriptScope" strong>スコープ</Term>と呼んでいます。</p> | ||
|
|
@@ -102,23 +118,57 @@ increment(); | |
|
|
||
| ::: | ||
|
|
||
| ## モジュール化 | ||
|
|
||
| 複雑な操作を <Term type="javascriptFunction">関数</Term> として <Term strong type="javascriptModularization">モジュール化</Term> して複数のブロックに分解することで、コードの可読性を上げることができます。 | ||
| ```javascript | ||
| // モジュール化前 | ||
| const stringToRepeat = "☆"; | ||
| for (let i = 0; i < 10; i++) { | ||
| let result = ""; | ||
| for (let j = 0; j < i; j++) { | ||
| result += stringToRepeat; | ||
| } | ||
| document.write(result); | ||
| document.write("<br>"); | ||
| } | ||
| ``` | ||
|
|
||
| ```javascript | ||
| // モジュール化後 | ||
| function repeat(stringToRepeat, times) { | ||
| let result = ""; | ||
| for (let j = 0; j < times; j++) { | ||
| result += stringToRepeat; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| for (let i = 0; i < 10; i++) { | ||
| document.write(repeat("☆", i)); | ||
| document.write("<br>"); | ||
| } | ||
| ``` | ||
| この例における`repeat()`<Term type="javascriptFunction">関数</Term>は、第一<Term type="javascriptParameter">引数</Term>の<Term type="javascriptString">文字列</Term>を第二<Term type="javascriptParameter">引数</Term>回だけ繰り返し足したものを返します。 | ||
|
|
||
| ## 演習 | ||
|
|
||
| 携帯電話料金を計算する<Term type="javascriptFunction">関数</Term>を作ってみましょう。 | ||
|
|
||
| ```javascript | ||
| function calculateCost(monthlyBandwidth) { | ||
| function calculateCost(monthlyDataUse) { | ||
| // ここに処理を書く | ||
| } | ||
|
|
||
| document.write(calculateCost(3.5)); | ||
| ``` | ||
|
|
||
| `calculateCost` は、<Term type="javascriptParameter">引数</Term>に月間転送量 `monthlyBandwidth` を取り、その月の携帯電話料金を<Term type="javascriptReturnValue">戻り値</Term>として<Term type="javascriptReturn">返す</Term><Term type="javascriptFunction">関数</Term>です。携帯電話料金は、下のルールで決定されるとします。 | ||
| `calculateCost` は、<Term type="javascriptParameter">引数</Term>に月間転送量 `monthlyDataUse` を取り、その月の携帯電話料金を<Term type="javascriptReturnValue">戻り値</Term>として<Term type="javascriptReturn">返す</Term><Term type="javascriptFunction">関数</Term>です。携帯電話料金は、下のルールで決定されるとします。 | ||
|
|
||
|
|
||
| > 月間転送量を _monthlyBandwidth_ (GB) とします。 | ||
| > | ||
| > - _monthlyBandwidth_ < 5.0 のとき、携帯電話料金は _monthlyBandwidth_ × 600 (円) | ||
| > - _monthlyBandwidth_ >= 5.0 のとき、携帯電話料金は 3000 (円) | ||
| > | ||
|
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. 不要な空白がありそう。 |
||
| > - 月間転送量 < 5.0 (GB) のとき、携帯電話料金は 月間転送量 × 600 (円/GB) | ||
| > - 月間転送量 >= 5.0 (GB) のとき、携帯電話料金は 3000 (円) | ||
| > | ||
|
|
||
| <ViewSource url={import.meta.url} path="_samples/mobile-phone-bill" /> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GitHubのキーワード検索で調べてみたんですが、
monthlyBandwidthだと407件、monthlyDataUsageだと83件、monthlyDataUseだと6件なので、monthlyBandwidthとかがいい気がします。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Monthly Bandwidthをgoogleで検索して上から4サイトくらい見ると、月間の交通量 = バックエンドというかホストというかサーバー側の データ使用量制限 / データ速度制限 のことらしいです
https://help.podbean.com/support/solutions/articles/25000004919-what-is-monthly-space-and-bandwidth-
https://tr-ex.me/%E7%BF%BB%E8%A8%B3/%E8%8B%B1%E8%AA%9E-%E6%97%A5%E6%9C%AC%E8%AA%9E/monthly+bandwidth#gref
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
たしかに、bandwidthはちょっと意味合いが違いますねm(_ _)m
monthlyDataUsageあたりが良さげ。