Skip to content

Commit 4f69c1f

Browse files
authored
Merge branch 'updated-add-more-prac-exercise-from-loop-to-array' into change-var-name-in-function-exercise
2 parents 6189679 + 9857a6c commit 4f69c1f

File tree

11 files changed

+47
-47
lines changed

11 files changed

+47
-47
lines changed

docs/1-trial-session/08-loop/index.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ document.write(product);
207207

208208
### 別解
209209

210-
複合演算子を使うと以下のようにも書けます
210+
複合代入演算子を使うと以下のようにも書けます
211211

212212
```javascript
213213
let product = 1;
@@ -227,10 +227,10 @@ document.write(product);
227227

228228
ある整数の変数 `n` が与えられたとき、その値が素数であるかどうか判定して表示する`for`文を書いてみましょう。
229229

230-
そして、`n`に 6, 11 ,57, 89 を入れてテストしてみましょう。
230+
そして、`n` に 6、11、57、89 を入れてテストしてみましょう。
231231

232232
:::info
233-
素数の定義は「1とその数以外の整数で割り切れない自然数」でしたね。
233+
素数の定義は「1より大きい自然数のうち、1とその数以外で割り切れないもの」でしたね。
234234

235235
`範囲内の全ての自然数 i に対して〇〇である` はどうやって評価すればよいでしょうか?
236236

@@ -245,6 +245,8 @@ document.write(product);
245245

246246
<Answer>
247247

248+
変数の、最後に代入した値のみを保持する性質を利用します。
249+
248250
```javascript
249251
let n = 57; // 任意の整数
250252

@@ -254,9 +256,9 @@ if (n <= 1) {
254256
isPrime = false;
255257
}
256258

257-
for (let i = 2; i < n; i += 1) {
258-
if (n % i == 0) {
259-
isPrime = false; // 変数は最後に代入した値のみを保持する。
259+
for (let i = 2; i < integer; i += 1) {
260+
if (n % i === 0) {
261+
isPrime = false;
260262
}
261263
}
262264

@@ -274,7 +276,7 @@ if (isPrime) {
274276
前項で割ったあまりが0でないこととの `&&` (AND) をとることで帰納的に求めることもできます。
275277

276278
```javascript
277-
let n = 89; //任意の整数
279+
let n = 89; // 任意の整数
278280

279281
let isPrime = true;
280282
if (n <= 1) {

docs/1-trial-session/09-functions/index.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,6 @@ function max(a, b) {
193193

194194
`a > b``true` の場合、if 構文内部の `return` で関数実行が中断されるため、`else` キーワードは必ずしも必要ではありません。そのため、以下のように書くこともできます。
195195

196-
:::
197-
198196
```javascript
199197
function max(a, b) {
200198
if (a > b) {
@@ -206,14 +204,14 @@ function max(a, b) {
206204

207205
<ViewSource url={import.meta.url} path="_samples/max-no-else" />
208206

207+
:::
208+
209209
</Answer>
210210

211211
## 中級演習
212212

213213
### 携帯電話料金
214214

215-
## 演習
216-
217215
携帯電話料金を計算する<Term type="javascriptFunction">関数</Term>を作ってみましょう。
218216

219217
```javascript

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: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ studentNames[1] = "内藤";
3333

3434
:::
3535

36-
`[``]` の中には非負整数値になる任意の式を記述できます。変数や`関数()`も式なので使用することが可能です
36+
`[``]` の中には非負整数値になる任意の式を記述できます。変数や関数呼び出しも式なので使用することが可能です
3737

3838
```javascript
3939
const six = 6;
@@ -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" />
@@ -147,7 +155,7 @@ document.write(text);
147155

148156
### 最大値
149157

150-
引数にひとつの配列が与えられたとき、その配列の最大値を求める関数 `arrayMax` を作成しましょう。
158+
引数にひとつの配列が与えられたとき、その配列の最大値を求める関数 `findMax` を作成しましょう。
151159

152160
:::note
153161

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

177185
```javascript
178-
function arrayMax(array) {
179-
if (array.length == 0) return; //空配列の例外処理
186+
function findMax(array) {
187+
if (array.length === 0) return; // 空配列の例外処理
180188
let maxValue = array[0];
181189
for (let i = 0; i < array.length; i += 1) {
182190
if (array[i] > maxValue) maxValue = array[i];
@@ -191,8 +199,6 @@ function arrayMax(array) {
191199

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

194-
---
195-
196202
:::info 別解 (参考)
197203

198204
[`array.reduce` メソッド](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) を使ってこのように書くこともできます。
@@ -203,7 +209,7 @@ function max(a, b) {
203209
else return b;
204210
}
205211

206-
function arrayMax(array) {
212+
function findMax(array) {
207213
if (array.length == 0) return; //空配列をエスケープ
208214
return array.reduce(max, array[0]);
209215
}

docs/1-trial-session/14-events/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,4 @@ HTMLファイルに一工夫が必要です。見えない`<div>` タグを用
107107

108108
</Details>
109109

110-
<ViewSource url={import.meta.url} path="_samples/project-jack-in-a-box/" />
110+
<ViewSource url={import.meta.url} path="_samples/project-jack-in-a-box/" />

docs/3-web-servers/04-server/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ app.listen(3000);
221221
```
222222

223223
```html title=template.ejs
224-
<!DOCTYPE html>
224+
<!doctype html>
225225
<html lang="ja">
226226
<head>
227227
<meta charset="utf-8" />

0 commit comments

Comments
 (0)