-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
84 lines (65 loc) · 1.88 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
76
77
78
79
80
81
82
83
84
const quiz = [
{
question: "4+4は?",
answers: [
"8",
"7",
"6",
"5"
],
correct: "8"
},{
question: "Javaで文字列を定義する場合は?",
answers: [
"int",
"boolean",
"sirial",
"String"
],
correct: "String"
},{
question: "継承(extends)を使うときはどのような場合?",
answers: [
"APIを使用する場合",
"既存のクラスに基づいて新たなクラスを定義する場合",
"別クラスのメソッドの変数を使用する場合",
"newされた直後に自動的に実行される。値をあらかじめセットしておく場合"
],
correct: "既存のクラスに基づいて新たなクラスを定義する場合"
}
]
const quizLength = quiz.length;
let quizIndex = 0;
let score = 0;
const $button = document.getElementsByTagName('button');
const buttonLength = $button.length;
const setupQuiz = () => {
document.getElementById("js-question").textContent = quiz[quizIndex].question;
let buttonIndex = 0;
let buttonLength = $button.length;
while(buttonIndex < buttonLength){
$button[buttonIndex].textContent = quiz[quizIndex].answers[buttonIndex];
buttonIndex++;
}
}
setupQuiz();
const clickHandler = (e) => {
if(quiz[quizIndex].correct === e.target.textContent){
window.alert('正解!');
score++;
} else {
window.alert('不正解!');
}
quizIndex++;
if(quizIndex < quizLength){
setupQuiz();
} else {
window.alert('終了! あなたの正解数は' + score + '/' + quizLength + 'です');
}
};
for(let handlerIndex = 0;handlerIndex < buttonLength; handlerIndex++){
$button[handlerIndex].addEventListener('click', (e)=>{
clickHandler(e);
});
}
//dfjj