質問編集履歴

1

2021/02/26 04:20

投稿

taka211
taka211

スコア17

test CHANGED
File without changes
test CHANGED
@@ -22,4 +22,94 @@
22
22
 
23
23
  ### 該当のソースコード
24
24
 
25
+
26
+
27
+ const question = '4は?';
28
+
29
+ const answers = [
30
+
31
+ '1',
32
+
33
+ '2'
34
+
35
+ '3',
36
+
37
+ '4'
38
+
39
+ ];
40
+
41
+
42
+
43
+ const $button = document.getElementsByTagName('button');
44
+
45
+ const correct = '4';
46
+
47
+
48
+
49
+ //クイズの問題文、選択を定義
50
+
51
+ const setupQuiz = () => {
52
+
53
+ document.getElementById('js-question').textContent = question;
54
+
55
+ let buttonIndex = 0;
56
+
57
+ while (buttonIndex < $button.length) {
58
+
59
+ $button[buttonIndex].textContent = answers[buttonIndex];
60
+
61
+
62
+
63
+ buttonIndex++;
64
+
65
+ }
66
+
67
+ }
68
+
69
+ setupQuiz();
70
+
71
+
72
+
73
+ const clickHandler = (e) => {
74
+
75
+ if (correct === e.target.textContent) {
76
+
77
+ window.alert('正解');
78
+
79
+ } else {
80
+
81
+ window.alert('不正解');
82
+
83
+ }
84
+
85
+ }
86
+
25
87
  console.log(e);
88
+
89
+
90
+
91
+
92
+
93
+ $button[0].addEventListener('click', (e) => {
94
+
95
+ clickHandler();
96
+
97
+ })
98
+
99
+ $button[1].addEventListener('click', (e) => {
100
+
101
+ clickHandler();
102
+
103
+ })
104
+
105
+ $button[2].addEventListener('click', (e) => {
106
+
107
+ clickHandler();
108
+
109
+ })
110
+
111
+ $button[3].addEventListener('click', (e) => {
112
+
113
+ clickHandler();
114
+
115
+ })