-
Notifications
You must be signed in to change notification settings - Fork 28
/
app.js
75 lines (63 loc) · 1.91 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const quiz = [
{
question: 'ゲーム史上、最も売れたゲーム機はどれ?',
answers: [ 'スーパーファミコン', 'PlayStation 2', 'ニンテンドーDS', 'Xbox 360'],
correct: 'ニンテンドーDS'
}, {
question: '糸井重里が企画に関わった、任天堂の看板ゲームといえば?',
answers: [ 'MOTHER2', 'スーパーマリオブラザーズ3', 'スーパードンキーコング', '星のカービィ'],
correct: 'MOTHER2'
}, {
question: 'ファイナルファンタジーⅣの主人公の名前は?',
answers: [ 'フリオニール', 'クラウド', 'ティーダ', 'セシル'],
correct: 'セシル'
}
];
const $window = window;
const $doc = document;
const $question = $doc.getElementById('js-question');
const $buttons = $doc.querySelectorAll('.btn');
const quizLen = quiz.length;
let quizCount = 0;
let score = 0;
const init = () => {
$question.textContent = quiz[quizCount].question;
const buttonLen = $buttons.length;
let btnIndex = 0;
while(btnIndex < buttonLen){
$buttons[btnIndex].textContent = quiz[quizCount].answers[btnIndex];
btnIndex++;
}
};
const goToNext = () => {
quizCount++;
if(quizCount < quizLen){
init(quizCount);
} else {
// $window.alert('クイズ終了!');
showEnd();
}
};
const judge = (elm) => {
if(elm.textContent === quiz[quizCount].correct){
$window.alert('正解!');
score++;
} else {
$window.alert('不正解!');
}
goToNext();
};
const showEnd = () => {
$question.textContent = '終了!あなたのスコアは' + score + '/' + quizLen + 'です';
const $items = $doc.getElementById('js-items');
$items.style.visibility = 'hidden';
};
init();
let answersIndex = 0;
let answersLen = quiz[quizCount].answers.length;
while(answersIndex < answersLen){
$buttons[answersIndex].addEventListener('click', (e) => {
judge(e.target);
});
answersIndex++;
}