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

質問編集履歴

4

修正

2020/07/01 06:06

投稿

begine
begine

スコア7

title CHANGED
File without changes
body CHANGED
@@ -37,12 +37,8 @@
37
37
 
38
38
  ```javascript
39
39
 
40
- 'use strict';
41
-
42
40
  {
43
41
  const btn = document.getElementById('btn');
44
- const result = document.getElementById('result');
45
- const scoreLabel = document.querySelector('#result > p');
46
42
  const timer = document.getElementById('timer'); //1
47
43
  const sbtn = document.getElementById('sbtn'); //2
48
44
 
@@ -52,12 +48,6 @@
52
48
  let timerId; //clearTimeoutの引数に渡す
53
49
 
54
50
 
55
-
56
- document.addEventListener('DOMContentLoaded', function() {
57
- startTime = Date.now();
58
- countDown();
59
- });
60
-
61
51
  function countDown() { // countDownの宣言
62
52
  timerId = setTimeout (function() {
63
53
  //timerIdをsetTimeoutの返り値として取得 次の処理を指定したミリ秒後に実行(1度だけ)
@@ -85,7 +75,51 @@
85
75
  });
86
76
 
87
77
 
78
+
79
+ function setQuiz() {
80
+ isAnswered = false;
88
81
 
82
+ question.textContent = quizSet[currentNum].q;
83
+
84
+ while (choices.firstChild) {
85
+ choices.removeChild(choices.firstChild);
86
+ }
87
+
88
+ const shuffledChoices = shuffle([...quizSet[currentNum].c]);
89
+ shuffledChoices.forEach(choice => {
90
+ const li = document.createElement('li');
91
+ li.textContent = choice;
92
+ li.addEventListener('click', () => {
93
+ checkAnswer(li);
94
+ });
95
+ choices.appendChild(li);
96
+ });
97
+
98
+ if (currentNum === quizSet.length - 1) {
99
+ btn.textContent = 'Show Score';
100
+ }
101
+
102
+ }
103
+
104
+ setQuiz();
105
+
106
+ btn.addEventListener('click', () => {
107
+ if (btn.classList.contains('disabled')) {
108
+ return;
109
+ }
110
+ btn.classList.add('disabled');
111
+
112
+ if (currentNum === quizSet.length - 1) {
113
+ scoreLabel.textContent = `Score: ${score} / ${quizSet.length}`;
114
+ result.classList.remove('hidden');
115
+ } else {
116
+ currentNum++;
117
+ setQuiz();
118
+ }
119
+ });
120
+ }
121
+
122
+
89
123
  ```
90
124
 
91
125
  ### 試したこと
@@ -94,4 +128,4 @@
94
128
 
95
129
  ### 補足情報(FW/ツールのバージョンなど)
96
130
 
97
- ここにより詳細な情報をしてい。
131
+ していただいたコードで解決できました

3

修正

2020/07/01 06:06

投稿

begine
begine

スコア7

title CHANGED
File without changes
body CHANGED
@@ -32,74 +32,14 @@
32
32
 
33
33
  ### 該当のソースコード
34
34
 
35
- ```html
36
- <!DOCTYPE html>
37
- <html lang="ja">
38
- <head>
39
- <meta charset="utf-8">
40
- <title>Quiz</title>
41
- <link rel="stylesheet" href="css/styles.css">
42
- </head>
43
- <body>
44
- <section class="container2"> <!-- スタート画面-->
45
- <p>ボタンクリックで時間制限のあるクイズが始まります。</p>
46
- <div class="sbtn_move">
47
- <button id="sbtn" type="button" onclick="location.href='quiz.html'">START</button>
48
- </div>
49
- </section>
50
- <section hidden>
51
- <div></div>
52
- </section>
53
35
 
54
- <div hidden id="question"></div> <!--要素を取得させるだけのため、非表示-->
55
- <div hidden id="choices"></div>
56
- <div hidden id="setQuiz"></div>
57
- <div hidden id="btn"></div> <!--要素を取得させるだけのため、非表示-->
58
36
 
59
- <script src="js/main.js"></script>
60
- </body>
61
- </html>
62
- ```
63
37
 
64
- ```html
65
-
66
- <!DOCTYPE html>
67
- <html lang="ja">
68
- <head>
69
- <meta charset="utf-8">
70
- <title>Quiz</title>
71
- <link rel="stylesheet" href="css/styles.css">
72
- </head>
73
- <body>
74
- <section class="container"> <!-- クイズ画面-->
75
- <p id="question"></p> <!--問題文,jsで扱いやすいようにidを使う。-->
76
- <ul id="choices"> <!--選択肢-->
77
- </ul>
78
- <div id="btn" class="disabled">Next</div> <!--次の問題に進むボタン,押せない状態のボタン-->
79
-
80
- <section class="container3">
81
- <div id="timer">00:00.000</div> <!--カウントダウンタイマーを表示-->
82
- </section>
83
- <section id="result" class="hidden"> <!--結果画面,最初はクラスで画面外へ隠す-->
84
- <p></p>
85
- <a href="index.html">Replay?</a> <!--ページ再読み込みでゲームを最初から始める-->
86
- </section>
87
- </section>
88
-
89
- <button hidden id="sbtn"></button> <!--要素を取得させるだけのため、このボタンは非表示-->
90
-
91
- <script src="js/main.js"></script>
92
- </body>
93
- </html>
94
- ```
95
-
96
38
  ```javascript
97
39
 
98
40
  'use strict';
99
41
 
100
42
  {
101
- const question = document.getElementById('question');
102
- const choices = document.getElementById('choices');
103
43
  const btn = document.getElementById('btn');
104
44
  const result = document.getElementById('result');
105
45
  const scoreLabel = document.querySelector('#result > p');
@@ -111,18 +51,7 @@
111
51
  const timeToCountDown = 3 * 1000; //1000 = 1秒
112
52
  let timerId; //clearTimeoutの引数に渡す
113
53
 
114
- /*constは初期化が必要 const X = 数値、もしくは初期化しない場合は let x; */
115
54
 
116
- function updateTimer(t) { //ミリ秒を渡すと、分や秒に直す。t でミリ秒を渡す。
117
- const d = new Date(t); //渡されたtで、Dateオブジェクトを作る。
118
- let m = d.getMinutes(); //このDateオブジェクトから「分や秒」を取り出す。
119
- let s = d.getSeconds();
120
- let ms = d.getMilliseconds();
121
- m = ('0' + m).slice(-2); //文字列と数値を連結させると文字列になる。
122
- s = ('0' + s).slice(-2); //文字列と数値を連結させると文字列になる。
123
- ms = ('00' + ms).slice(-3); //文字列と数値を連結させると文字列になる。
124
- timer.textContent = m + ':' + s + '.' + ms; //timerの中身を書き換え。
125
- }
126
55
 
127
56
  document.addEventListener('DOMContentLoaded', function() {
128
57
  startTime = Date.now();

2

修正

2020/07/01 05:58

投稿

begine
begine

スコア7

title CHANGED
File without changes
body CHANGED
@@ -156,80 +156,7 @@
156
156
  });
157
157
 
158
158
 
159
- const quizSet = shuffle([
160
- {q: '世界で一番大きな湖は?', c: ['カスピ海', 'カリブ海', '琵琶湖']},
161
- {q: '2の8乗は?', c: ['256', '64', '1024']},
162
- {q: '次のうち、最初にリリースされた言語は?', c: ['Python', 'JavaScript', 'HTML']},
163
- ]);
164
- let currentNum = 0;
165
- let isAnswered;
166
- let score = 0;
167
159
 
168
- function shuffle(arr) {
169
- for (let i = arr.length - 1; i > 0; i--) {
170
- const j = Math.floor(Math.random() * (i + 1));
171
- [arr[j], arr[i]] = [arr[i], arr[j]];
172
- }
173
- return arr;
174
- }
175
-
176
- function checkAnswer(li) {
177
- if (isAnswered) {
178
- return;
179
- }
180
- isAnswered = true;
181
-
182
- if (li.textContent === quizSet[currentNum].c[0]) {
183
- li.classList.add('correct');
184
- score++;
185
- } else {
186
- li.classList.add('wrong');
187
- }
188
-
189
- btn.classList.remove('disabled');
190
- }
191
-
192
- function setQuiz() {
193
- isAnswered = false;
194
-
195
- question.textContent = quizSet[currentNum].q;
196
-
197
- while (choices.firstChild) {
198
- choices.removeChild(choices.firstChild);
199
- }
200
-
201
- const shuffledChoices = shuffle([...quizSet[currentNum].c]);
202
- shuffledChoices.forEach(choice => {
203
- const li = document.createElement('li');
204
- li.textContent = choice;
205
- li.addEventListener('click', () => {
206
- checkAnswer(li);
207
- });
208
- choices.appendChild(li);
209
- });
210
-
211
- if (currentNum === quizSet.length - 1) {
212
- btn.textContent = 'Show Score';
213
- }
214
- }
215
-
216
- setQuiz();
217
-
218
- btn.addEventListener('click', () => {
219
- if (btn.classList.contains('disabled')) {
220
- return;
221
- }
222
- btn.classList.add('disabled');
223
-
224
- if (currentNum === quizSet.length - 1) {
225
- scoreLabel.textContent = `Score: ${score} / ${quizSet.length}`;
226
- result.classList.remove('hidden');
227
- } else {
228
- currentNum++;
229
- setQuiz();
230
- }
231
- });
232
- }
233
160
  ```
234
161
 
235
162
  ### 試したこと

1

修正

2020/06/28 09:14

投稿

begine
begine

スコア7

title CHANGED
File without changes
body CHANGED
@@ -142,7 +142,6 @@
142
142
  clearTimeout(timerId);
143
143
  timeLeft = 0; //timeLeftを0にして、updateTimerで更新する
144
144
  updateTimer(timeLeft);
145
- result();
146
145
  return; //clearTimeout()したら次のcountDown()を呼び出したくないためreturnする
147
146
  }
148
147