質問編集履歴

2

文章の変更

2019/09/08 08:05

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- 制限時間をつける方法を知り
1
+ 定義されていないというエラーが出まし
test CHANGED
@@ -1,473 +1,479 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- 今、クイズアプリを作り終わったのですが、オリジナルとして制限時間をつけいをいま
3
+ 今、クイズアプリを作り終わ、オリジナルとして制限時間をつける途中なのですが、下の回答者のようにコードてみたらエラーが出した
4
4
 
5
5
 
6
6
 
7
7
  ### 発生している問題・エラーメッセージ
8
8
 
9
+ ![](7b9f62509db67880dc04657fccde37bb.png)
10
+
11
+
12
+
13
+ ### 該当のソースコード
14
+
15
+
16
+
17
+ ```html
18
+
19
+ <!DOCTYPE html>
20
+
21
+ <html lang="ja">
22
+
23
+ <head>
24
+
25
+ <meta charset="utf-8">
26
+
27
+ <title>Quiz</title>
28
+
29
+ <link rel="stylesheet" href="css/styles.css">
30
+
31
+ </head>
32
+
33
+ <body>
34
+
35
+ <div class="container">
36
+
37
+ <p id="question"></p>
38
+
39
+ <ul id="choices"></ul>
40
+
41
+ <div id="btn" class="disabled">Next</div>
42
+
43
+
44
+
45
+ <section id="result">
46
+
47
+ <p></p>
48
+
49
+ <a href="">Replay?</a>
50
+
51
+ </section>
52
+
53
+ </div>
54
+
55
+
56
+
57
+ <script src="js/main.js"></script>
58
+
59
+ </body>
60
+
61
+ </html>
62
+
9
63
 
10
64
 
11
65
  ```
12
66
 
67
+
68
+
13
- 特にないです
69
+ ```css
70
+
71
+ body {
72
+
73
+ background: #efdec1;
74
+
75
+ font-size: 14px;
76
+
77
+ font-family: Verdana, sans-serif;
78
+
79
+ }
80
+
81
+
82
+
83
+ .container {
84
+
85
+ width: 400px;
86
+
87
+ margin: 8px auto;
88
+
89
+ background: #fff;
90
+
91
+ border-radius: 4px;
92
+
93
+ padding: 16px;
94
+
95
+ position: relative;
96
+
97
+ }
98
+
99
+
100
+
101
+ #question {
102
+
103
+ margin-bottom: 16px;
104
+
105
+ font-weight: bold;
106
+
107
+ }
108
+
109
+
110
+
111
+ #choices {
112
+
113
+ list-style: none;
114
+
115
+ padding: 0;
116
+
117
+ margin-bottom: 16px;
118
+
119
+ }
120
+
121
+
122
+
123
+ #choices > li {
124
+
125
+ border: 1px solid #ccc;
126
+
127
+ border-radius: 4px;
128
+
129
+ padding: 10px;
130
+
131
+ margin-bottom: 10px;
132
+
133
+ cursor: pointer;
134
+
135
+ }
136
+
137
+
138
+
139
+ #choices > li:hover {
140
+
141
+ background: #f8f8f8;
142
+
143
+ }
144
+
145
+
146
+
147
+ #choices > li.correct {
148
+
149
+ background: #d4edda;
150
+
151
+ border-color: #c3e6cb;
152
+
153
+ color: #155724;
154
+
155
+ font-weight: bold;
156
+
157
+ }
158
+
159
+
160
+
161
+ #choices > li.correct::after {
162
+
163
+ content: " ... correct!";
164
+
165
+ }
166
+
167
+
168
+
169
+ #choices > li.wrong {
170
+
171
+ background: #f8d8da;
172
+
173
+ border-color: #f5c6cb;
174
+
175
+ color: #721c24;
176
+
177
+ font-weight: bold;
178
+
179
+ }
180
+
181
+
182
+
183
+ #choices > li.wrong::after {
184
+
185
+ content: " ... wrong!";
186
+
187
+ }
188
+
189
+
190
+
191
+ #btn, #result > a {
192
+
193
+ background: #3498db;
194
+
195
+ padding: 8px;
196
+
197
+ border-radius: 4px;
198
+
199
+ cursor: pointer;
200
+
201
+ text-align: center;
202
+
203
+ color: #fff;
204
+
205
+ box-shadow: 0 4px 0 #2880b9;
206
+
207
+ }
208
+
209
+
210
+
211
+ #btn.disabled {
212
+
213
+ background: #ccc;
214
+
215
+ box-shadow: 0 4px 0 #bbb;
216
+
217
+ opacity: 0.7;
218
+
219
+ }
220
+
221
+
222
+
223
+ #result {
224
+
225
+ position: absolute;
226
+
227
+ width: 300px;
228
+
229
+ background: #fff;
230
+
231
+ padding: 30px;
232
+
233
+ box-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
234
+
235
+ /* top: 50px; */
236
+
237
+ top: -500px;
238
+
239
+ left: 0;
240
+
241
+ right: 0;
242
+
243
+ margin: 0 auto;
244
+
245
+ border-radius: 3px;
246
+
247
+ text-align: center;
248
+
249
+ transition: 0.4s ease-out;
250
+
251
+ }
252
+
253
+
254
+
255
+ #result.show {
256
+
257
+ top: 50px;
258
+
259
+ }
260
+
261
+
262
+
263
+ #result > p {
264
+
265
+ font-size: 24px;
266
+
267
+ }
268
+
269
+
270
+
271
+ #result > a {
272
+
273
+ display: block;
274
+
275
+ text-decoration: none;
276
+
277
+ }
278
+
279
+
14
280
 
15
281
  ```
16
282
 
17
283
 
18
284
 
19
- ### 該当のソースコード
20
-
21
-
22
-
23
- ```html
24
-
25
- <!DOCTYPE html>
26
-
27
- <html lang="ja">
28
-
29
- <head>
30
-
31
- <meta charset="utf-8">
32
-
33
- <title>Quiz</title>
34
-
35
- <link rel="stylesheet" href="css/styles.css">
36
-
37
- </head>
38
-
39
- <body>
40
-
41
- <div class="container">
42
-
43
- <p id="question"></p>
44
-
45
- <ul id="choices"></ul>
46
-
47
- <div id="btn" class="disabled">Next</div>
48
-
49
-
50
-
51
- <section id="result">
52
-
53
- <p></p>
54
-
55
- <a href="">Replay?</a>
56
-
57
- </section>
58
-
59
- </div>
60
-
61
-
62
-
63
- <script src="js/main.js"></script>
64
-
65
- </body>
66
-
67
- </html>
285
+ ```js
286
+
287
+ 'use strict';
288
+
289
+
290
+
291
+ {
292
+
293
+ const question = document.getElementById('question');
294
+
295
+ const btn = document.getElementById('btn');
296
+
297
+ const choices = document.getElementById('choices');
298
+
299
+ const result = document.getElementById('result');
300
+
301
+ const scoreLabel = document.querySelector('#result > p');
302
+
303
+
304
+
305
+ const quizSet = [
306
+
307
+ // {q: 'What is A?', c: ['A0', 'A1', 'A2']},
308
+
309
+ // {q: 'What is B?', c: ['B0', 'B1', 'B2']},
310
+
311
+ // {q: 'What is C?', c: ['C0', 'C1', 'C2']},
312
+
313
+ {q: '世界で一番大きな湖は?', c: ['カスピ海', 'カリブ海', '琵琶湖']},
314
+
315
+ {q: '2の8乗は?', c: ['256', '64', '1024']},
316
+
317
+ {q: 'いま、何問目?', c: ['3問目', '4問目', '5問目']},
318
+
319
+ ];
320
+
321
+ let currentNum = 0;
322
+
323
+ let isAnswered;
324
+
325
+ let score = 0;
326
+
327
+
328
+
329
+ function shuffle(arr) {
330
+
331
+ for (let i = arr.length - 1; i > 0; i--) {
332
+
333
+ const j = Math.floor(Math.random() * (i + 1));
334
+
335
+ [arr[j], arr[i]] = [arr[i], arr[j]];
336
+
337
+ }
338
+
339
+ return arr;
340
+
341
+ }
342
+
343
+
344
+
345
+ function checkAnswer(li) {
346
+
347
+ if (isAnswered) {
348
+
349
+ return;
350
+
351
+ }
352
+
353
+ isAnswered = true;
354
+
355
+
356
+
357
+ if (li.textContent === quizSet[currentNum].c[0]) {
358
+
359
+ li.classList.add('correct');
360
+
361
+ score++;
362
+
363
+ } else {
364
+
365
+ li.classList.add('wrong');
366
+
367
+ }
368
+
369
+
370
+
371
+ btn.classList.remove('disabled');
372
+
373
+ }
374
+
375
+
376
+
377
+ function setQuiz() {
378
+
379
+ isAnswered = false;
380
+
381
+
382
+
383
+ question.textContent = quizSet[currentNum].q;
384
+
385
+
386
+
387
+ while (choices.firstChild) {
388
+
389
+ choices.removeChild(choices.firstChild);
390
+
391
+ }
392
+
393
+
394
+
395
+ const shuffledChoices = shuffle([...quizSet[currentNum].c]);
396
+
397
+ shuffledChoices.forEach(choice => {
398
+
399
+ const li = document.createElement('li');
400
+
401
+ li.textContent = choice;
402
+
403
+ li.addEventListener('click', () => {
404
+
405
+ checkAnswer(li);
406
+
407
+ });
408
+
409
+ choices.appendChild(li);
410
+
411
+ });
412
+
413
+
414
+
415
+ if (currentNum === quizSet.length - 1) {
416
+
417
+ btn.textContent = 'Show Score';
418
+
419
+ }
420
+
421
+ }
422
+
423
+
424
+
425
+ setQuiz();
426
+
427
+
428
+
429
+ btn.addEventListener('click', () => {
430
+
431
+ if (btn.classList.contains('disabled')) {
432
+
433
+ return;
434
+
435
+ }
436
+
437
+ btn.classList.add('disabled');
438
+
439
+
440
+
441
+ if (currentNum === quizSet.length - 1) {
442
+
443
+ // console.log(`Score: ${score} / ${quizSet.length}`);
444
+
445
+ scoreLabel.textContent = `Score: ${score} / ${quizSet.length}`;
446
+
447
+ result.classList.add('show');
448
+
449
+ } else {
450
+
451
+ currentNum++;
452
+
453
+ setQuiz();
454
+
455
+ }
456
+
457
+ });
458
+
459
+
460
+
461
+ function newi(){
462
+
463
+ document.getElementById("choices").style.display ="none";
464
+
465
+ }
466
+
467
+
468
+
469
+ setTimeout("newi()", 10000);
470
+
471
+ }
68
472
 
69
473
 
70
474
 
71
475
  ```
72
476
 
73
-
74
-
75
- ```css
76
-
77
- body {
78
-
79
- background: #efdec1;
80
-
81
- font-size: 14px;
82
-
83
- font-family: Verdana, sans-serif;
84
-
85
- }
86
-
87
-
88
-
89
- .container {
90
-
91
- width: 400px;
92
-
93
- margin: 8px auto;
94
-
95
- background: #fff;
96
-
97
- border-radius: 4px;
98
-
99
- padding: 16px;
100
-
101
- position: relative;
102
-
103
- }
104
-
105
-
106
-
107
- #question {
108
-
109
- margin-bottom: 16px;
110
-
111
- font-weight: bold;
112
-
113
- }
114
-
115
-
116
-
117
- #choices {
118
-
119
- list-style: none;
120
-
121
- padding: 0;
122
-
123
- margin-bottom: 16px;
124
-
125
- }
126
-
127
-
128
-
129
- #choices > li {
130
-
131
- border: 1px solid #ccc;
132
-
133
- border-radius: 4px;
134
-
135
- padding: 10px;
136
-
137
- margin-bottom: 10px;
138
-
139
- cursor: pointer;
140
-
141
- }
142
-
143
-
144
-
145
- #choices > li:hover {
146
-
147
- background: #f8f8f8;
148
-
149
- }
150
-
151
-
152
-
153
- #choices > li.correct {
154
-
155
- background: #d4edda;
156
-
157
- border-color: #c3e6cb;
158
-
159
- color: #155724;
160
-
161
- font-weight: bold;
162
-
163
- }
164
-
165
-
166
-
167
- #choices > li.correct::after {
168
-
169
- content: " ... correct!";
170
-
171
- }
172
-
173
-
174
-
175
- #choices > li.wrong {
176
-
177
- background: #f8d8da;
178
-
179
- border-color: #f5c6cb;
180
-
181
- color: #721c24;
182
-
183
- font-weight: bold;
184
-
185
- }
186
-
187
-
188
-
189
- #choices > li.wrong::after {
190
-
191
- content: " ... wrong!";
192
-
193
- }
194
-
195
-
196
-
197
- #btn, #result > a {
198
-
199
- background: #3498db;
200
-
201
- padding: 8px;
202
-
203
- border-radius: 4px;
204
-
205
- cursor: pointer;
206
-
207
- text-align: center;
208
-
209
- color: #fff;
210
-
211
- box-shadow: 0 4px 0 #2880b9;
212
-
213
- }
214
-
215
-
216
-
217
- #btn.disabled {
218
-
219
- background: #ccc;
220
-
221
- box-shadow: 0 4px 0 #bbb;
222
-
223
- opacity: 0.7;
224
-
225
- }
226
-
227
-
228
-
229
- #result {
230
-
231
- position: absolute;
232
-
233
- width: 300px;
234
-
235
- background: #fff;
236
-
237
- padding: 30px;
238
-
239
- box-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
240
-
241
- /* top: 50px; */
242
-
243
- top: -500px;
244
-
245
- left: 0;
246
-
247
- right: 0;
248
-
249
- margin: 0 auto;
250
-
251
- border-radius: 3px;
252
-
253
- text-align: center;
254
-
255
- transition: 0.4s ease-out;
256
-
257
- }
258
-
259
-
260
-
261
- #result.show {
262
-
263
- top: 50px;
264
-
265
- }
266
-
267
-
268
-
269
- #result > p {
270
-
271
- font-size: 24px;
272
-
273
- }
274
-
275
-
276
-
277
- #result > a {
278
-
279
- display: block;
280
-
281
- text-decoration: none;
282
-
283
- }
284
-
285
-
286
-
287
- ```
288
-
289
-
290
-
291
- ```js
292
-
293
- 'use strict';
294
-
295
-
296
-
297
- {
298
-
299
- const question = document.getElementById('question');
300
-
301
- const btn = document.getElementById('btn');
302
-
303
- const choices = document.getElementById('choices');
304
-
305
- const result = document.getElementById('result');
306
-
307
- const scoreLabel = document.querySelector('#result > p');
308
-
309
-
310
-
311
- const quizSet = [
312
-
313
- // {q: 'What is A?', c: ['A0', 'A1', 'A2']},
314
-
315
- // {q: 'What is B?', c: ['B0', 'B1', 'B2']},
316
-
317
- // {q: 'What is C?', c: ['C0', 'C1', 'C2']},
318
-
319
- {q: '世界で一番大きな湖は?', c: ['カスピ海', 'カリブ海', '琵琶湖']},
320
-
321
- {q: '2の8乗は?', c: ['256', '64', '1024']},
322
-
323
- {q: 'いま、何問目?', c: ['3問目', '4問目', '5問目']},
324
-
325
- ];
326
-
327
- let currentNum = 0;
328
-
329
- let isAnswered;
330
-
331
- let score = 0;
332
-
333
-
334
-
335
- function shuffle(arr) {
336
-
337
- for (let i = arr.length - 1; i > 0; i--) {
338
-
339
- const j = Math.floor(Math.random() * (i + 1));
340
-
341
- [arr[j], arr[i]] = [arr[i], arr[j]];
342
-
343
- }
344
-
345
- return arr;
346
-
347
- }
348
-
349
-
350
-
351
- function checkAnswer(li) {
352
-
353
- if (isAnswered) {
354
-
355
- return;
356
-
357
- }
358
-
359
- isAnswered = true;
360
-
361
-
362
-
363
- if (li.textContent === quizSet[currentNum].c[0]) {
364
-
365
- li.classList.add('correct');
366
-
367
- score++;
368
-
369
- } else {
370
-
371
- li.classList.add('wrong');
372
-
373
- }
374
-
375
-
376
-
377
- btn.classList.remove('disabled');
378
-
379
- }
380
-
381
-
382
-
383
- function setQuiz() {
384
-
385
- isAnswered = false;
386
-
387
-
388
-
389
- question.textContent = quizSet[currentNum].q;
390
-
391
-
392
-
393
- while (choices.firstChild) {
394
-
395
- choices.removeChild(choices.firstChild);
396
-
397
- }
398
-
399
-
400
-
401
- const shuffledChoices = shuffle([...quizSet[currentNum].c]);
402
-
403
- shuffledChoices.forEach(choice => {
404
-
405
- const li = document.createElement('li');
406
-
407
- li.textContent = choice;
408
-
409
- li.addEventListener('click', () => {
410
-
411
- checkAnswer(li);
412
-
413
- });
414
-
415
- choices.appendChild(li);
416
-
417
- });
418
-
419
-
420
-
421
- if (currentNum === quizSet.length - 1) {
422
-
423
- btn.textContent = 'Show Score';
424
-
425
- }
426
-
427
- }
428
-
429
-
430
-
431
- setQuiz();
432
-
433
-
434
-
435
- btn.addEventListener('click', () => {
436
-
437
- if (btn.classList.contains('disabled')) {
438
-
439
- return;
440
-
441
- }
442
-
443
- btn.classList.add('disabled');
444
-
445
-
446
-
447
- if (currentNum === quizSet.length - 1) {
448
-
449
- // console.log(`Score: ${score} / ${quizSet.length}`);
450
-
451
- scoreLabel.textContent = `Score: ${score} / ${quizSet.length}`;
452
-
453
- result.classList.add('show');
454
-
455
- } else {
456
-
457
- currentNum++;
458
-
459
- setQuiz();
460
-
461
- }
462
-
463
- });
464
-
465
- }
466
-
467
-
468
-
469
- ```
470
-
471
477
  ### 試したこと
472
478
 
473
479
  色々関連するサイトを調べたりしました。
@@ -489,3 +495,7 @@
489
495
  ](https://dotinstall.com/lessons/quiz_js_v2)
490
496
 
491
497
  上のサイトで作りました。
498
+
499
+
500
+
501
+ 何かわかる方ご教授お願いします。

1

文章の変更

2019/09/08 08:04

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -479,3 +479,13 @@
479
479
  macbook air 10.14.6
480
480
 
481
481
  atomのバージョン 1.40.1x64(?)
482
+
483
+
484
+
485
+ ### サイト
486
+
487
+ [JavaScriptで三択クイズを作ろう
488
+
489
+ ](https://dotinstall.com/lessons/quiz_js_v2)
490
+
491
+ 上のサイトで作りました。