This repository has been archived by the owner on Apr 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod1quiz.html
220 lines (194 loc) · 6.67 KB
/
mod1quiz.html
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<html>
<head>
<h1 align="center">LEVEL 3 MODULE 1 QUIZ</h1>
</head>
<body onload="startup()" style="background-color:#bbccdd;font-weight:bold;font-size:22px;">
<pre style="white-space:pre-wrap;"><code>
<div id="docBody"style="overflow:visible;"></div>
</code></pre>
</body>
<script>
function Question(){
this.question = "";
this.choices = [];
this.correctAnswer = "";
this.userAnswer = "";
this.visited = false;
}
var body;
var questions = [];
var currentQuestion;
function startup(){
currentQuestion = 0;
body = document.getElementById("docBody");
questions[0] = new Question();
questions[0].question = "What is the name of the method that needs to be written to make the following test pass?<br><br>"+
"@Test<br>"+
"public void testMethod(){<br>"+
"\tassertEquals(16, quadrupleNumber(4));<br>"+
"}" ;
questions[0].choices[0] = "public";
questions[0].choices[1] = "void";
questions[0].choices[2] = "testMethod";
questions[0].choices[3] = "assertEquals";
questions[0].choices[4] = "quadrupleNumber";
questions[0].correctAnswer = 4;
questions[1] = new Question();
questions[1].question = "Given the following line of code, what should be the return type for the numberToWords method?<br><br>"+
"assertEquals(\“seven\”, numberToWords(7));";
questions[1].choices[0] = "int";
questions[1].choices[1] = "String";
questions[1].choices[2] = "Object";
questions[1].choices[3] = "boolean";
questions[1].choices[4] = "void";
questions[1].correctAnswer = 1;
questions[2] = new Question();
questions[2].question = "Given the following line of code, what should be the return type for the isAlphanumeric method?<br><br>"+
"assertTrue(isAlphanumeric(‘c’));" ;
questions[2].choices[0] = "int";
questions[2].choices[1] = "String";
questions[2].choices[2] = "Object";
questions[2].choices[3] = "boolean";
questions[2].choices[4] = "void";
questions[2].correctAnswer = 3;
questions[3] = new Question();
questions[3].question = "Will the following JUnit test pass?<br><br>"+
"@Test<br>"+
"public void testMethod(){<br>"+
"\tassertEquals(5, add(2, 2));<br>"+
"}<br><br>"+
"public int add(int x, int y){ return x + y; }";
questions[3].choices[0] = "yes";
questions[3].choices[1] = "no";
questions[3].choices[2] = "it is impossible to say";
questions[3].choices[3] = "an error would occur";
questions[3].choices[4] = "most likely";
questions[3].correctAnswer = 1;
setDivText(questions[0]);
setButtons(questions[0]);
}
function setDivText(q){
body.innerHTML += currentQuestion + 1 + ".<br>";
body.innerHTML += q.question + "<br><br><hr size=\"1\">";
if(q.choices.length == 0){
body.innerHTML += "<br><textarea id=\"answer\" rows=\"10\" cols=\"80\">" +
q.userAnswer + "</textarea><br>";
}else{
for(var i = 0; i < q.choices.length; i++){
if(i == q.userAnswer && q.visited){
body.innerHTML += convertChar(i) + "<input type=\"radio\" checked=\"true\"name=\"answer\" id=\"" + i + "\">" +q.choices[i] + "</input><br><br>";
}else{
body.innerHTML += "<input type=\"radio\" name=\"answer\" id=\"" + i + "\"><br>" +q.choices[i] + "</input><br><hr size=\"1\">";
}
}
q.visited = true;
}
}
function convertChar(a){
switch(a){
case 0:
case "0": return "A";
case 1:
case "1": return "B";
case 2:
case "2": return "C";
case 3:
case "3": return "D";
case 4:
case "4": return "E";
}
}
function setButtons(q){
if(q == questions[0]){
if(questions.length > 1){
body.innerHTML += "<button onclick=\"nextQuestion()\">NEXT</button>";
}else{
body.innerHTML += "<button onclick=\"submit()\">SUBMIT</button>";
}
}
else if(q == questions[questions.length - 1]){
body.innerHTML += "<button onclick=\"previousQuestion()\">PREVIOUS</button>";
body.innerHTML += "<button onclick=\"submit()\">SUBMIT</button>";
}else{
body.innerHTML += "<button onclick=\"previousQuestion()\">PREVIOUS</button>";
body.innerHTML += "<button onclick=\"nextQuestion()\">NEXT</button>";
}
}
function nextQuestion(){
if(setUserAnswer()){
body.innerHTML = "";
currentQuestion++;
setDivText(questions[currentQuestion]);
setButtons(questions[currentQuestion]);
}
}
function previousQuestion(){
if(setUserAnswer()){
body.innerHTML = "";
currentQuestion--;
setDivText(questions[currentQuestion]);
setButtons(questions[currentQuestion]);
}
}
function setUserAnswer(){
var q = questions[currentQuestion];
if(q.choices.length > 0){
try{
q.userAnswer = document.querySelector('input[name="answer"]:checked').id;
return true;
}catch(err){
alert("Make a choice. You can come back and change it later.");
return false;
}
}else{
q.userAnswer = document.getElementById("answer").value.trim();
return true;
}
}
function checkQuiz(){
var total = 0;
for(var i = 0; i < questions.length; i++){
var q = questions[i];
if(q.choices.length == 0){
if(q.userAnswer.trim() == q.correctAnswer){
total++;
}
} else {
if(q.correctAnswer == q.userAnswer){
total++;
}
}
}
return total;
}
function reloadPage(){
location = location;
}
function submit(){
setUserAnswer();
body.innerHTML = "You got " + checkQuiz() + " out of " + questions.length + " correct.<br><hr size=\"3\">";
for(var i = 0; i < questions.length; i++){
if(questions[i].choices.length > 0){
body.innerHTML += i + 1 + ". ";
body.innerHTML += questions[i].question + "<br><br>";
body.innerHTML += "Your Answer: <br>";
body.innerHTML += questions[i].choices[questions[i].userAnswer] + "<br>";
body.innerHTML += "Correct Answer: <br>";
body.innerHTML += questions[i].choices[questions[i].correctAnswer] + "<br>";
body.innerHTML += "<hr size=\"3\">";
}else{
body.innerHTML += i + 1 + ". ";
body.innerHTML += questions[i].question + "<br><br>";
body.innerHTML += "Your Answer: <br>";
body.innerHTML += questions[i].userAnswer + "<br>";
body.innerHTML += "Correct Answer: <br>";
body.innerHTML += questions[i].correctAnswer + "<br>";
body.innerHTML += "<hr size=\"3\">";
}
}
body.innerHTML += "<button onclick=\"reloadPage()\">START OVER</button>";
}
</script>
<script src="http://league-level0.github.io/copyrightFooter.js"></script>
<div id="copyright"><script>copyright();</script></div>
</html>