質問編集履歴
3
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -90,7 +90,7 @@
|
|
90
90
|
### 補足情報(FW/ツールのバージョンなど)
|
91
91
|
|
92
92
|
|
93
|
-
ブロック
|
93
|
+
ブロック内で以下のように記述したら解決できました。
|
94
94
|
|
95
95
|
```javascript
|
96
96
|
showResult(); // 仮
|
2
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -35,17 +35,17 @@
|
|
35
35
|
const correct = document.getElementById('correct'); // 仮
|
36
36
|
const wrong = document.getElementById('wrong'); // 仮
|
37
37
|
|
38
|
-
|
38
|
+
function countDown() {
|
39
|
-
|
39
|
+
timerId = setTimeout (function() {
|
40
40
|
timeLeft = timeToCountDown - (Date.now() - startTime);
|
41
|
-
|
42
41
|
|
42
|
+
|
43
43
|
if(timeLeft < 0){
|
44
44
|
clearTimeout(timerId);
|
45
45
|
timeLeft = 0;
|
46
46
|
|
47
47
|
updateTimer(timeLeft);
|
48
|
-
|
48
|
+
|
49
49
|
btn.classList.remove('disabled');
|
50
50
|
btn.textContent = '結果発表';
|
51
51
|
currentNum = quizSet.length - 1;
|
@@ -64,65 +64,6 @@
|
|
64
64
|
const accuracy = correct + wrong === 0 ? 0 : correct / (correct + wrong) * 100;
|
65
65
|
}
|
66
66
|
|
67
|
-
|
68
|
-
|
69
|
-
sbtn.addEventListener('click', function() {
|
70
|
-
startTime = Date.now();
|
71
|
-
countDown();
|
72
|
-
});
|
73
|
-
|
74
|
-
|
75
|
-
function shuffle(arr) {
|
76
|
-
for (let i = arr.length - 1; i > 0; i--) {
|
77
|
-
const j = Math.floor(Math.random() * (i + 1));
|
78
|
-
[arr[j], arr[i]] = [arr[i], arr[j]];
|
79
|
-
}
|
80
|
-
return arr;
|
81
|
-
}
|
82
|
-
|
83
|
-
function checkAnswer(li) {
|
84
|
-
if (isAnswered) {
|
85
|
-
return;
|
86
|
-
}
|
87
|
-
isAnswered = true;
|
88
|
-
|
89
|
-
if (li.textContent === quizSet[currentNum].c[0]) {
|
90
|
-
li.classList.add('correct');
|
91
|
-
score++;
|
92
|
-
} else {
|
93
|
-
li.classList.add('wrong');
|
94
|
-
}
|
95
|
-
|
96
|
-
btn.classList.remove('disabled');
|
97
|
-
}
|
98
|
-
|
99
|
-
function setQuiz() {
|
100
|
-
isAnswered = false;
|
101
|
-
|
102
|
-
question.textContent = quizSet[currentNum].q;
|
103
|
-
|
104
|
-
while (choices.firstChild) {
|
105
|
-
choices.removeChild(choices.firstChild);
|
106
|
-
}
|
107
|
-
|
108
|
-
const shuffledChoices = shuffle([...quizSet[currentNum].c]);
|
109
|
-
shuffledChoices.forEach(choice => {
|
110
|
-
const li = document.createElement('li');
|
111
|
-
li.textContent = choice;
|
112
|
-
li.addEventListener('click', () => {
|
113
|
-
checkAnswer(li);
|
114
|
-
if (currentNum === quizSet.length - 1) clearTimeout(timerId);
|
115
|
-
});
|
116
|
-
choices.appendChild(li);
|
117
|
-
});
|
118
|
-
|
119
|
-
if (currentNum === quizSet.length - 1) {
|
120
|
-
btn.textContent = '結果発表';
|
121
|
-
}
|
122
|
-
}
|
123
|
-
|
124
|
-
setQuiz();
|
125
|
-
|
126
67
|
btn.addEventListener('click', () => {
|
127
68
|
if (btn.classList.contains('disabled')) {
|
128
69
|
return;
|
@@ -149,7 +90,7 @@
|
|
149
90
|
### 補足情報(FW/ツールのバージョンなど)
|
150
91
|
|
151
92
|
|
152
|
-
以下のように記述したら解決
|
93
|
+
ブロックスコープ内で以下のように記述したら解決できました。
|
153
94
|
|
154
95
|
```javascript
|
155
96
|
showResult(); // 仮
|
1
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -15,110 +15,26 @@
|
|
15
15
|
|
16
16
|
### 該当のソースコード
|
17
17
|
|
18
|
-
```html
|
19
|
-
<!DOCTYPE html>
|
20
|
-
<html lang="ja">
|
21
|
-
<head>
|
22
|
-
<meta charset="utf-8">
|
23
|
-
<title>Quiz</title>
|
24
|
-
<link rel="stylesheet" href="css/styles.css">
|
25
|
-
</head>
|
26
|
-
<body>
|
27
|
-
<section class="container2"> <!-- スタート画面-->
|
28
|
-
<p>ボタンクリックで時間制限のあるクイズが始まります。</p>
|
29
|
-
<div class="sbtn_move">
|
30
|
-
<button id="sbtn" type="button" onclick="location.href='quiz.html'">START</button>
|
31
|
-
</div>
|
32
|
-
</section>
|
33
|
-
<section hidden>
|
34
|
-
<div></div>
|
35
|
-
</section>
|
36
18
|
|
37
|
-
<div hidden id="question"></div> <!--要素を取得させるだけのため、非表示-->
|
38
|
-
<div hidden id="choices"></div>
|
39
|
-
<div hidden id="setQuiz"></div>
|
40
|
-
<div hidden id="timer"></div>
|
41
|
-
<div hidden id="btn"></div> <!--要素を取得させるだけのため、非表示-->
|
42
|
-
|
43
|
-
<script src="js/main.js"></script>
|
44
|
-
</body>
|
45
|
-
</html>
|
46
|
-
|
47
|
-
```
|
48
|
-
|
49
19
|
```html
|
50
20
|
|
51
|
-
<!DOCTYPE html>
|
52
|
-
<html lang="ja">
|
53
|
-
<head>
|
54
|
-
<meta charset="utf-8">
|
55
|
-
<title>Quiz</title>
|
56
|
-
<link rel="stylesheet" href="css/styles.css">
|
57
|
-
</head>
|
58
|
-
<body>
|
59
|
-
<section class="container"> <!-- クイズ画面-->
|
60
|
-
<p id="question"></p> <!--問題文,jsで扱いやすいようにidを使う。-->
|
61
|
-
<ul id="choices"> <!--選択肢-->
|
62
|
-
</ul>
|
63
|
-
<div id="btn" class="disabled">Next</div> <!--次の問題に進むボタン,押せない状態のボタン-->
|
64
21
|
|
65
22
|
<section class="container3">
|
66
23
|
<div id="timer">00:00.00</div> <!--カウントダウンタイマーを表示-->
|
67
24
|
<div hidden id="correct"></div> <!-- 仮 -->
|
68
25
|
<div hidden id="wrong"></div> <!-- 仮 -->
|
69
26
|
</section>
|
70
|
-
|
27
|
+
|
71
|
-
<p></p>
|
72
|
-
<a href="index.html">Replay?</a> <!--ページ再読み込みでゲームを最初から始める-->
|
73
|
-
</section>
|
74
|
-
</section>
|
75
28
|
|
76
|
-
<button hidden id="sbtn"></button> <!--要素を取得させるだけのため、このボタンは非表示-->
|
77
|
-
|
78
|
-
<script src="js/main.js"></script>
|
79
|
-
</body>
|
80
|
-
</html>
|
81
|
-
|
82
29
|
```
|
83
30
|
|
84
31
|
```javascript
|
85
32
|
|
86
|
-
'use strict';
|
87
33
|
|
88
|
-
|
34
|
+
|
89
|
-
const question = document.getElementById('question');
|
90
|
-
const choices = document.getElementById('choices');
|
91
|
-
const btn = document.getElementById('btn');
|
92
|
-
const result = document.getElementById('result');
|
93
|
-
const scoreLabel = document.querySelector('#result > p');
|
94
|
-
const timer = document.getElementById('timer');
|
95
|
-
const sbtn = document.getElementById('sbtn');
|
96
35
|
const correct = document.getElementById('correct'); // 仮
|
97
36
|
const wrong = document.getElementById('wrong'); // 仮
|
98
37
|
|
99
|
-
let startTime; //STARTボタンを押したときの時刻(宣言)
|
100
|
-
let timeLeft; //残り時間
|
101
|
-
const timeToCountDown = 5 * 1000;
|
102
|
-
let timerId; //clearTimeoutの引数に渡す
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
function updateTimer(t) {
|
107
|
-
const d = new Date(t);
|
108
|
-
let m = d.getMinutes();
|
109
|
-
let s = d.getSeconds();
|
110
|
-
let ms = d.getMilliseconds();
|
111
|
-
m = ('0' + m).slice(-2);
|
112
|
-
s = ('0' + s).slice(-2);
|
113
|
-
ms = ('00' + ms).slice(-3);
|
114
|
-
timer.textContent = m + ':' + s + '.' + ms;
|
115
|
-
}
|
116
|
-
|
117
|
-
document.addEventListener('DOMContentLoaded', function() {
|
118
|
-
startTime = Date.now();
|
119
|
-
countDown();
|
120
|
-
});
|
121
|
-
|
122
38
|
function countDown() {
|
123
39
|
timerId = setTimeout (function() {
|
124
40
|
timeLeft = timeToCountDown - (Date.now() - startTime);
|
@@ -156,15 +72,6 @@
|
|
156
72
|
});
|
157
73
|
|
158
74
|
|
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
|
-
|
168
75
|
function shuffle(arr) {
|
169
76
|
for (let i = arr.length - 1; i > 0; i--) {
|
170
77
|
const j = Math.floor(Math.random() * (i + 1));
|
@@ -241,4 +148,24 @@
|
|
241
148
|
|
242
149
|
### 補足情報(FW/ツールのバージョンなど)
|
243
150
|
|
151
|
+
|
244
|
-
|
152
|
+
以下のように記述したら解決することができました。
|
153
|
+
|
154
|
+
```javascript
|
155
|
+
showResult(); // 仮
|
156
|
+
|
157
|
+
function showResult(){ // 仮
|
158
|
+
const accuracy = correct + wrong === 0 ? 0 : correct / (correct + wrong) * 100;
|
159
|
+
if (currentNum === quizSet.length - 1) {
|
160
|
+
scoreLabel.innerHTML = `${quizSet.length} <br>
|
161
|
+
${score} <br>
|
162
|
+
${accuracy.toFixed(2)}%`; //仮
|
163
|
+
result.classList.remove('hidden');
|
164
|
+
} else {
|
165
|
+
currentNum++;
|
166
|
+
setQuiz();
|
167
|
+
}
|
168
|
+
}
|
169
|
+
});
|
170
|
+
}
|
171
|
+
```
|