forked from jchoi8163/Pollution-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz.html
More file actions
119 lines (103 loc) · 4.23 KB
/
quiz.html
File metadata and controls
119 lines (103 loc) · 4.23 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<html>
<style>
#body1{
background-color:gray;
}
.jumbotron{
text-align: center;
background-color:black;
color:gray;
}
#radio{
height: 150px;
width: 500px;
max-width:500px;
margin: auto;
}
</style>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/2.3.0/mustache.min.js"></script>
<script type = "text/Template" id = "Template">
<div id ="radio">
<p>Question {{qNum}}: {{info.Question}}</p>
<form id = "radioAnswers">
<div><input type="radio" name="choices" value="A" style="width:18px; height:18px;">A) {{info.A}}</div>
<div><input type="radio" name="choices" value="B" style="width:18px; height:18px;">B) {{info.B}}</div>
<div><input type="radio" name="choices" value="C" style="width:18px; height:18px;">C) {{info.C}}</div>
<div><input type="radio" name="choices" value="D" style="width:18px; height:18px;">D) {{info.D}}</div>
</form>
</div>
</script>
<script>
var data;
function getData(){
$.getJSON("https://project1-98c41.firebaseio.com/.json", function(result){
data = result;
displayQuestions();
});
}
function displayQuestions(){
var build ="";
var template = document.getElementById("Template").innerHTML;
var qNumber = 0; // Question number
for(var key in data){
qNumber++;
quesJSON ={
"info": data[key],
"qNum": qNumber
}
build += Mustache.render(template, quesJSON);
}
document.getElementById("quiz").innerHTML = build;
}
// CHECK ANSWERS AND CALCULATE SCORE
function checkAnswers(){
var score = 0;
var count = 0;
for(var key in data){
for(var i = 0; i< 4; i++){
if(document.getElementById("radioAnswers").elements.choices[i].checked === true){
var userAnswer = document.getElementById("radioAnswers").elements.choices[i].value;
if(userAnswer == data[key].Answer){
score++
}
}
}
count++
var grd = (score/count * 100).toFixed(0);
}
document.getElementById("score").innerHTML = "<h1>You answered " + score + " out of " + count+" questions correctly!<br><br>That's a grade of<b> "+ grd + "%</b></h1>";
}
</script>
<body id = "body1" onload = "getData()">
<!--Jumbotron Header-->
<div class="container">
<div class="jumbotron">
<h1>Quiz</h1>
<p></p>
</div>
</div>
<!--NavTab-->
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">GoGreen♲</a>
</div>
<ul class="nav navbar-nav">
<li><a href="index.html">Home</a></li>
<li><a href="water.html">Water Pollution</a></li>
<li><a href="forest.html">Deforestation</a></li>
<li><a href="air.html">Air Pollution</a></li>
<li class="active"><a href="quiz.html">Quiz</a></li>
</ul>
</div>
</nav>
<div id="quiz"></div>
<button onclick="checkAnswers()" type="button" class="btn btn-success btn-lg">SCORE QUIZ</button>
<div id="score"></div>
</body>
</html>