質問編集履歴

2

タイトル修正。

2019/05/09 13:32

投稿

amaturePy
amaturePy

スコア131

test CHANGED
@@ -1 +1 @@
1
- JSのアラートで問題の正当数が上手く機能しません。デバック依頼。
1
+ JSのアラートで問題の正当数が上手く機能しません。
test CHANGED
File without changes

1

タイトルをデバック依頼へ修正。インデントの修正。DB情報の追加。

2019/05/09 13:31

投稿

amaturePy
amaturePy

スコア131

test CHANGED
@@ -1 +1 @@
1
- JSの返り値をチェックして頂けないでしょうか
1
+ JSのアラート問題の正当数が上手く機能ませんデバック依頼。
test CHANGED
@@ -2,7 +2,11 @@
2
2
 
3
3
  返り値のコードがおかしいのか、正解の選択肢を入れて回答しても正当数0で表示されてしまいます。
4
4
 
5
- JSにはあまり詳しくなく、どなたかお力をお借りできないでしょうか?```ここに言語を入力
5
+ JSにはあまり詳しくなく、どなたかお力をお借りできないでしょうか?```
6
+
7
+ <テーブル:quiz>
8
+
9
+ <カラム:id / question / choice_1 / choice_2 / choice_3 / answer />
6
10
 
7
11
  ```ここに言語を入力
8
12
 
@@ -10,187 +14,183 @@
10
14
 
11
15
  <!DOCTYPE html>
12
16
 
13
- <html>
17
+     <html>
14
18
 
15
- <head>
19
+     <head>
16
20
 
17
- <title>Sample Quiz</title>
21
+     <title>Sample Quiz</title>
18
22
 
19
- </head>
23
+     </head>
20
24
 
21
- <body>
25
+     <body>
22
26
 
23
27
 
24
28
 
25
- <?php
29
+     <?php
26
30
 
27
- $dsn='mysql:host=localhost;dbname=quiz';
31
+     $dsn='mysql:host=localhost;dbname=quiz';
28
32
 
29
- $user='root';
33
+      $user='root';
30
34
 
31
- $password='root';
35
+      $password='root';
32
36
 
33
37
 
34
38
 
35
39
  try{
36
40
 
37
- $db=new PDO($dsn,$user,$password);
41
+   $db=new PDO($dsn,$user,$password);
38
42
 
39
- $db ->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
43
+    $db ->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
40
44
 
41
- $sql="SELECT * FROM quiz ORDER BY RAND()";
45
+     $sql="SELECT * FROM quiz ORDER BY RAND()";
42
46
 
43
- $stmt=$db->query($sql);
47
+   $stmt=$db->query($sql);
44
48
 
45
- $stmt->execute();
49
+   $stmt->execute();
46
50
 
47
- }catch(PDOException $error){
51
+   }catch(PDOException $error){
48
52
 
49
- echo "error".$error->getMessage();
53
+     echo "error".$error->getMessage();
50
54
 
51
- }
55
+    }
52
56
 
53
57
  ///////////////////////////////////////////////////////////////////////////// DB
54
58
 
55
- $id='id';
59
+   $id='id';
56
60
 
57
- $question='question';
61
+   $question='question';
58
62
 
59
- $choice_1='choice_1';
63
+    $choice_1='choice_1';
60
64
 
61
- $choice_2='choice_2';
65
+    $choice_2='choice_2';
62
66
 
63
- $choice_3='choice_3';
67
+    $choice_3='choice_3';
64
68
 
65
- $number=0;
69
+    $number=0;
66
70
 
67
71
 
68
72
 
69
- $number = 0;
73
+    $number = 0;
70
74
 
71
- for($i=0;$row=$stmt->fetch(); $i++){
75
+    for($i=0;$row=$stmt->fetch(); $i++){
72
76
 
73
- $number++;
77
+    $number++;
74
78
 
75
- $id = $row['id'];
79
+    $id = $row['id'];
76
80
 
77
- $question = $row['question'];
81
+    $question = $row['question'];
78
82
 
79
- $ans_array = array($row['choice_1'],$row['choice_2'],$row['choice_3']);{
83
+    $ans_array = array($row['choice_1'],$row['choice_2'],$row['choice_3']);{
80
84
 
81
85
  }
82
86
 
83
- shuffle($ans_array);
87
+    shuffle($ans_array);
84
88
 
85
- ?>
89
+     ?>
86
90
 
87
91
 
88
92
 
89
- <h4> <?php echo $number . ".) " . $question; ?></h4>
93
+     <h4> <?php echo $number . ".) " . $question; ?></h4>
90
94
 
91
- <label><input type="radio" value="<?php echo $ans_array[0]; ?>" name="<?php echo $question; ?>"> <?php echo $ans_array[0]; ?></label>
95
+     <label><input type="radio" value="<?php echo $ans_array[0]; ?>" name="<?php       echo $question; ?>"> <?php echo $ans_array[0]; ?></label>
92
96
 
93
- <label><input type="radio" value="<?php echo $ans_array[1]; ?>" name="<?php echo $question; ?>"> <?php echo $ans_array[1]; ?></label>
97
+     <label><input type="radio" value="<?php echo $ans_array[1]; ?>" name="<?php echo      $question; ?>"> <?php echo $ans_array[1]; ?></label>
94
98
 
95
- <label><input type="radio" value="<?php echo $ans_array[2]; ?>" name="<?php echo $question; ?>"> <?php echo $ans_array[2]; ?></label>
99
+     <label><input type="radio" value="<?php echo $ans_array[2]; ?>" name="<?php echo      $question; ?>"> <?php echo $ans_array[2]; ?></label>
96
100
 
97
101
 
98
102
 
99
- <?php
103
+    <?php}?>
100
-
101
- }
102
-
103
- ?>
104
104
 
105
105
 
106
106
 
107
- <br><br>
107
+    <br><br>
108
108
 
109
109
 
110
110
 
111
- <button onclick="returnScore()">View Results</button>
111
+     <button onclick="returnScore()">View Results</button>
112
112
 
113
113
 
114
114
 
115
- <script> ///////////////////////////////////////////////////////////////////// Showing total score
115
+ <script> ///////////////////////////////////////////////////////////////////// Showing     total score
116
116
 
117
- var ans = [];
117
+      var ans = [];
118
118
 
119
- var answers = [];
119
+       var answers = [];
120
120
 
121
- answers[0]="Japanese";
121
+      answers[0]="Japanese";
122
122
 
123
- answers[1]="Sushi";
123
+      answers[1]="Sushi";
124
124
 
125
- answers[2]="Abe";
125
+      answers[2]="Abe";
126
126
 
127
- answers[3]="Yen";
127
+       answers[3]="Yen";
128
128
 
129
- answers[4]="JAL";
129
+       answers[4]="JAL";
130
130
 
131
- answers[5]="Tokyo";
131
+       answers[5]="Tokyo";
132
132
 
133
- answers[6]="Naruto";
133
+       answers[6]="Naruto";
134
134
 
135
- answers[7]="Reiwa";
135
+       answers[7]="Reiwa";
136
136
 
137
- answers[8]="Line";
137
+       answers[8]="Line";
138
138
 
139
- answers[9]="Kento";
139
+       answers[9]="Kento";
140
140
 
141
141
 
142
142
 
143
- function getScore(){{
143
+       function getScore(){{
144
144
 
145
- var score=0;
145
+       var score=0;
146
146
 
147
- var numQuestion=10;
147
+       var numQuestion=10;
148
148
 
149
- return score ;
149
+       return score ;
150
150
 
151
- return numQuestion ;
151
+       return numQuestion ;
152
152
 
153
- }
153
+      }
154
154
 
155
- score = getScore();
155
+       score = getScore();
156
156
 
157
157
 
158
158
 
159
159
 
160
160
 
161
- for(var i=0;i<=numQuestion;i++){
161
+       for(var i=0;i<=numQuestion;i++){
162
162
 
163
- if(ans[i]==answers[i]){
163
+       if(ans[i]==answers[i]){
164
164
 
165
- score+=1;
165
+       score+=1;
166
166
 
167
- }
167
+    }
168
168
 
169
- else{
169
+      else{
170
170
 
171
- score +=0;
171
+      score +=0;
172
172
 
173
- }
173
+    }
174
174
 
175
175
  }
176
176
 
177
177
  }
178
178
 
179
- function returnScore(){
179
+       function returnScore(){
180
180
 
181
- console.log('test')
181
+        console.log('test')
182
182
 
183
- alert("Your Score is" + getScore()+"/10");
183
+       alert("Your Score is" + getScore()+"/10");
184
184
 
185
185
  }
186
186
 
187
187
 
188
188
 
189
- </script>
189
+     </script>
190
190
 
191
- </body>
191
+     </body>
192
192
 
193
- </html>
193
+     </html>
194
194
 
195
195
  ```
196
196