-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
209 lines (187 loc) · 7.84 KB
/
index.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
<head>
<h1 align="center">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 = "Consider the following method oddArray, which changes every even number value in the array to 0. By the end of the method, only odd numbers will be present in the array. Which line correctly completes /* to be determined */ to make the code work as intended?\n\npublic void oddArray (int[] arr)\n{\n for (int i = 0; i < arr.length; i++)\n {\n //if the number at arr[i] is even, it becomes 0\n if( /* to be determined */ )\n arr[i] = 0;\n }\n}";
questions[0].choices[0] = "arr[i] / 2 = 2";
questions[0].choices[1] = "arr[i] % 2 == 1";
questions[0].choices[2] = "arr[i] / 2 == 1";
questions[0].choices[3] = "arr[i] % 2 == 0";
questions[0].choices[4] = "arr[i] / 2 == 0";
questions[0].correctAnswer = 3;
questions[1] = new Question();
questions[1].question = "The method numFun is below. What is returned as a result of numFun(21560)?\n\npublic static int numFun(int num)\n{\n if (num / 10 == 0)\n return num;\n\n else\n return (num % 10) + numFun(num / 10);\n}";
questions[1].choices[0] = "4";
questions[1].choices[1] = "5";
questions[1].choices[2] = "0";
questions[1].choices[3] = "13";
questions[1].choices[4] = "14";
questions[1].correctAnswer = 4;
questions[2] = new Question();
questions[2].question = "Consider the method emptyList, shown below. The method returns true if a List of integers is filled with zeros and false otherwise. Which of the following should replace /* to be completed */ so that the method will work as intended?\n\npublic boolean emptyList (List list)\n{\n /* to be completed */\n}\n\n// I.\nfor (int i = 0; i < list.size(); i++)\n{\n if (list.get(i) != 0)\n return false;\n}\nreturn true;\n\n// II.\nfor (int i = 0; i < list.size(); i++)\n{\n if (list[i] != 0)\n return false;\n}\nreturn true;\n\n// III.\nfor (int i = 0; i < list.size(); i++)\n{\n if (list.get(i) != 0)\n return true;\n}\nreturn false;";
questions[2].choices[0] = "I only";
questions[2].choices[1] = "II only";
questions[2].choices[2] = "III only";
questions[2].choices[3] = "I and II only";
questions[2].choices[4] = "II and III only";
questions[2].correctAnswer = 0;
questions[3] = new Question();
questions[3].question = "You need to find a random integer in the range 1 to 25, inclusive. Which of the following always returns a value that satisfies this condition? ";
questions[3].choices[0] = "(int) (Math.random() * 25) * 1";
questions[3].choices[1] = "(int) (Math.random() + 1) * 25";
questions[3].choices[2] = "(int) (Math.random() + 25) * 1";
questions[3].choices[3] = "(int) (Math.random()) * 25 + 1";
questions[3].choices[4] = "(int) (Math.random() * 25) + 1";
questions[3].correctAnswer = 4;
questions[4] = new Question();
questions[4].question = "A list of 120 names has been sorted in alphabetical order. Using a binary search method, what is the minimum number of passes needed to find a specified name or confirm that it is not in the list? ";
questions[4].choices[0] = "5";
questions[4].choices[1] = "7";
questions[4].choices[2] = "10";
questions[4].choices[3] = "12";
questions[4].choices[4] = "128";
questions[4].correctAnswer = 1;
setDivText(questions[0]);
setButtons(questions[0]);
}
function setDivText(q){
body.innerHTML += currentQuestion + 1 + ". ";
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 += convertChar(questions[i].userAnswer) + "<br>";
body.innerHTML += "Correct Answer: <br>";
body.innerHTML += convertChar(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>