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

質問編集履歴

1

コードの追加

2021/01/03 16:19

投稿

nomunen
nomunen

スコア0

title CHANGED
File without changes
body CHANGED
@@ -6,7 +6,7 @@
6
6
  ### 発生している問題・エラーメッセージ
7
7
 
8
8
  ```
9
-
9
+ 問題をランダムに出題できない、問題開始のボタンともう一度最初から解くボタンの付け方がわからない
10
10
  ```
11
11
 
12
12
  ### 該当のソースコード
@@ -14,7 +14,97 @@
14
14
  ```ここに言語名を入力
15
15
  javascript
16
16
 
17
+ var a = 'ゲームを開始します。';
18
+ alert (a);
17
19
 
20
+ const quiz = [
21
+ {
22
+ question: '動物',
23
+ answers: ['animal',
24
+ 'house',
25
+ 'car',
26
+ 'camera'
27
+ ],
28
+ correct: 'animal'
29
+ }, {
30
+ question: '車',
31
+ answers: ['animal',
32
+ 'house',
33
+ 'car',
34
+ 'camera'
35
+ ],
36
+ correct: 'car'
37
+ }, {
38
+ question: '家',
39
+ answers: ['animal',
40
+ 'house',
41
+ 'car',
42
+ 'camera'
43
+ ],
44
+ correct: 'house'
45
+ }, {
46
+ question: 'カメラ',
47
+ answers: ['animal',
48
+ 'house',
49
+ 'car',
50
+ 'camera'
51
+ ],
52
+ correct: 'camera'
53
+ }
54
+ ];
55
+
56
+ const quizLength = quiz.length;
57
+ let quizIndex = 0;
58
+ let score = 0;
59
+
60
+ const button = document.getElementsByTagName('button');
61
+ const buttonLength = button.length;
62
+
63
+ //クイズの問題文、選択肢を定義
64
+ const setupQuiz = () => {
65
+ document.getElementById('js-question').textContent = quiz[quizIndex].question;
66
+ let buttonIndex = 0;
67
+ while(buttonIndex < buttonLength){
68
+ button[buttonIndex].textContent = quiz[quizIndex].answers[buttonIndex];
69
+ buttonIndex++;
70
+ }
71
+ }
72
+
73
+ setupQuiz();
74
+
75
+ const clickhandler = (e) => {
76
+ if(quiz[quizIndex].correct === e.target.textContent){
77
+ window.alert('正解!');
78
+ score++;
79
+ } else {
80
+ window.alert('不正解!');
81
+ }
82
+ quizIndex++;
83
+
84
+ if(quizIndex < quizLength){
85
+ //問題数がまだあればこちらを実行
86
+ setupQuiz();
87
+ } else {
88
+ //問題数がもうなければこちらを実行
89
+ window.alert('お疲れ様でした。あなたの正解数は' + score + '/' + quizLength + 'です。');
90
+ }
91
+ };
92
+
93
+ //ボタンをクリックしたら正誤判定
94
+ let handlerIndex = 0;
95
+ while (handlerIndex < buttonLength){
96
+ button[handlerIndex].addEventListener('click', (e) => {clickhandler(e);
97
+ });
98
+ handlerIndex++;
99
+ }
100
+
101
+ var gaol = 10;
102
+
103
+ var progress = 0;
104
+
105
+ while(goal > progress) {
106
+ var result = Math.floor(Math.random() * 6) + 1;
107
+ }
18
108
  ```
19
109
 
20
110
  ### 試したこと