teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

5

追記

2019/09/08 11:29

投稿

kyoya0819
kyoya0819

スコア10434

answer CHANGED
@@ -17,4 +17,201 @@
17
17
  「関数の定義ができないならしなければいいじゃない」(某漫画をオマージュ)
18
18
  ```JS
19
19
  window.setTimeout(function(){document.getElementById("choices").style.display ="none";},2000);
20
- ```
20
+ ```
21
+
22
+ ### 追記
23
+
24
+ 質問内容からずれますがこんなのはどうでしょうか?
25
+
26
+ ```HTML
27
+ <html lang="ja">
28
+ <head>
29
+
30
+ <meta charset="utf-8">
31
+ <title>Quiz</title>
32
+ <link rel="stylesheet" href="css/styles.css">
33
+ </head>
34
+ <body>
35
+ <div class="container">
36
+ <p id="question"></p>
37
+ <ul id="choices"></ul>
38
+
39
+ <section id="result">
40
+ <p></p>
41
+ <a href="">Replay?</a>
42
+ </section>
43
+ </div>
44
+
45
+ <script src="js/main.js"></script>
46
+ </body>
47
+ </html>
48
+ ```
49
+ ```CSS
50
+ body {
51
+ background: #efdec1;
52
+ font-size: 14px;
53
+ font-family: Verdana, sans-serif;
54
+ }
55
+
56
+ .container {
57
+ width: 400px;
58
+ margin: 8px auto;
59
+ background: #fff;
60
+ border-radius: 4px;
61
+ padding: 16px;
62
+ position: relative;
63
+ }
64
+
65
+ #question {
66
+ margin-bottom: 16px;
67
+ font-weight: bold;
68
+ }
69
+
70
+ #choices {
71
+ list-style: none;
72
+ padding: 0;
73
+ margin-bottom: 16px;
74
+ }
75
+
76
+ #choices > li {
77
+ border: 1px solid #ccc;
78
+ border-radius: 4px;
79
+ padding: 10px;
80
+ margin-bottom: 10px;
81
+ cursor: pointer;
82
+ }
83
+
84
+ #choices > li:hover {
85
+ background: #f8f8f8;
86
+ }
87
+
88
+ #choices > li.correct {
89
+ background: #d4edda;
90
+ border-color: #c3e6cb;
91
+ color: #155724;
92
+ font-weight: bold;
93
+ }
94
+
95
+ #choices > li.correct::after {
96
+ content: " ... correct!";
97
+ }
98
+
99
+ #choices > li.wrong {
100
+ background: #f8d8da;
101
+ border-color: #f5c6cb;
102
+ color: #721c24;
103
+ font-weight: bold;
104
+ }
105
+
106
+ #choices > li.wrong::after {
107
+ content: " ... wrong!";
108
+ }
109
+
110
+ #result > a {
111
+ background: #3498db;
112
+ padding: 8px;
113
+ border-radius: 4px;
114
+ cursor: pointer;
115
+ text-align: center;
116
+ color: #fff;
117
+ box-shadow: 0 4px 0 #2880b9;
118
+ }
119
+
120
+ #result {
121
+ position: absolute;
122
+ width: 300px;
123
+ background: #fff;
124
+ padding: 30px;
125
+ box-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
126
+ /* top: 50px; */
127
+ top: -500px;
128
+ left: 0;
129
+ right: 0;
130
+ margin: 0 auto;
131
+ border-radius: 3px;
132
+ text-align: center;
133
+ transition: 0.4s ease-out;
134
+ }
135
+
136
+ #result.show {
137
+ top: 50px;
138
+ }
139
+
140
+ #result > p {
141
+ font-size: 24px;
142
+ }
143
+
144
+ #result > a {
145
+ display: block;
146
+ text-decoration: none;
147
+ }
148
+ ```
149
+ ```JS
150
+ 'use strict';
151
+
152
+ {
153
+ const question = document.getElementById('question');
154
+ const choices = document.getElementById('choices');
155
+ const result = document.getElementById('result');
156
+ const scoreLabel = document.querySelector('#result > p');
157
+
158
+ const quizSet = [
159
+ // {q: 'What is A?', c: ['A0', 'A1', 'A2']},
160
+ // {q: 'What is B?', c: ['B0', 'B1', 'B2']},
161
+ // {q: 'What is C?', c: ['C0', 'C1', 'C2']},
162
+ {q: '世界で一番大きな湖は?', c: ['カスピ海', 'カリブ海', '琵琶湖']},
163
+ {q: '2の8乗は?', c: ['256', '64', '1024']},
164
+ {q: 'いま、何問目?', c: ['3問目', '4問目', '5問目']},
165
+ ];
166
+ let currentNum = 0;
167
+ let isAnswered;
168
+ let score = 0;
169
+
170
+ function shuffle(arr) {
171
+ for (let i = arr.length - 1; i > 0; i--) {
172
+ const j = Math.floor(Math.random() * (i + 1));
173
+ [arr[j], arr[i]] = [arr[i], arr[j]];
174
+ }
175
+ return arr;
176
+ }
177
+
178
+ function checkAnswer(li) {
179
+ if (isAnswered) {
180
+ return;
181
+ }
182
+ isAnswered = true;
183
+
184
+ if (li.textContent === quizSet[currentNum].c[0]) {
185
+ li.classList.add('correct');
186
+ score++;
187
+ } else {
188
+ li.classList.add('wrong');
189
+ }
190
+
191
+ }
192
+
193
+ function setQuiz() {
194
+ isAnswered = false;
195
+
196
+ question.textContent = quizSet[currentNum].q;
197
+
198
+ while (choices.firstChild) {
199
+ choices.removeChild(choices.firstChild);
200
+ }
201
+
202
+ const shuffledChoices = shuffle([...quizSet[currentNum].c]);
203
+ shuffledChoices.forEach(choice => {
204
+ const li = document.createElement('li');
205
+ li.textContent = choice;
206
+ li.addEventListener('click', () => {
207
+ checkAnswer(li);
208
+ });
209
+ choices.appendChild(li);
210
+ });
211
+ }
212
+
213
+ setQuiz();
214
+ window.setTimeout(function(){currentNum++;setQuiz();window.setTimeout(function(){currentNum++;setQuiz();window.setTimeout(function(){scoreLabel.textContent=`Score: ${score} / ${quizSet.length}`;result.classList.add('show');},5000);},5000);},5000);
215
+ }
216
+ ```
217
+ [サンプル](https://s.codepen.io/asuchi0819/debug/VwZQbMV/XBkGRqgpjLZA)

4

追記

2019/09/08 11:29

投稿

kyoya0819
kyoya0819

スコア10434

answer CHANGED
@@ -12,4 +12,9 @@
12
12
  }
13
13
 
14
14
  window.setTimeout("newi()", 10000);
15
+ ```
16
+
17
+ 「関数の定義ができないならしなければいいじゃない」(某漫画をオマージュ)
18
+ ```JS
19
+ window.setTimeout(function(){document.getElementById("choices").style.display ="none";},2000);
15
20
  ```

3

失敬

2019/09/08 08:30

投稿

kyoya0819
kyoya0819

スコア10434

answer CHANGED
@@ -11,5 +11,5 @@
11
11
  document.getElementById("choices").style.display ="none";
12
12
  }
13
13
 
14
- setTimeout("newi()", 10000);
14
+ window.setTimeout("newi()", 10000);
15
15
  ```

2

こんなとこか?

2019/09/08 08:15

投稿

kyoya0819
kyoya0819

スコア10434

answer CHANGED
@@ -1,3 +1,15 @@
1
1
  制限時間を付ける方法
2
2
 
3
- 一定時間後JavaScriptで要素を非表示にしたりして操作できなくする。(本格的なものを作りたいなら不十分)
3
+ 一定時間後JavaScriptで要素を非表示にしたりして操作できなくする。(本格的なものを作りたいなら不十分)
4
+
5
+ ```HTML
6
+ <ul id="choices"></ul>
7
+ ```
8
+ を非表示にしたいなら
9
+ ```JavaScript
10
+ function newi(){
11
+ document.getElementById("choices").style.display ="none";
12
+ }
13
+
14
+ setTimeout("newi()", 10000);
15
+ ```

1

追記

2019/09/08 07:46

投稿

kyoya0819
kyoya0819

スコア10434

answer CHANGED
@@ -1,3 +1,3 @@
1
1
  制限時間を付ける方法
2
2
 
3
- JavaScriptで要素を非表示にしたりして操作できなくする。(本格的なものを作りたいなら不十分)
3
+ 一定時間後JavaScriptで要素を非表示にしたりして操作できなくする。(本格的なものを作りたいなら不十分)