-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
154 lines (128 loc) · 4.15 KB
/
script.js
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
var questions = [];
var totalQuestionNr = 0;
var currentQuestionNr = 0;
var correctAnswersCount = 0;
function _(x) {
return document.getElementById(x);
}
function loadQuestionsAndStartQuiz(value) {
$.getJSON('https://katecpp.github.io/qt_quiz/question.json', function (data) {
questions = data.quiz;
})
.error(function(){
_("content").innerHTML = "<h3>Ooops... something went wrong!</h3>" +
"<p>Questions could not be loaded.<br>" +
"Maybe your browser does not support jQuery 1.7.1.</p>";})
.done(function() {
startQuizInternal(value);
});
}
function printQuestion() {
var questionsDiv = _("question");
var i = 0;
_("status").innerHTML = "<h3>Question " + (currentQuestionNr+1) + " of " + totalQuestionNr + "</h3>";
questionsDiv.innerHTML = questions[currentQuestionNr].question + "</br>";
for (i = 0; i < questions[currentQuestionNr].answers.length; i++)
{
questionsDiv.innerHTML += "<input type='radio' name='choices' value='" +
questions[currentQuestionNr].answers[i].key + "'> " +
questions[currentQuestionNr].answers[i].txt + "<br>";
}
questionsDiv.innerHTML += "<button onclick='printAnswer()' id='submitButton' class='smallBtn'>Submit</button>";
_("hint").innerHTML = "";
}
var getUserAnswer = function() {
var choices = document.getElementsByName("choices");
for(var i=0; i<choices.length; i++)
{
if(choices[i].checked)
{
return choices[i].value;
}
}
return undefined;
}
var checkAnswer = function() {
var userChoice = getUserAnswer();
if (undefined === userChoice)
{
alert("Choose answer!");
return undefined;
}
else if(userChoice === questions[currentQuestionNr].correct)
{
correctAnswersCount++;
return true;
}
return false;
}
function printAnswer() {
var userAnswer = checkAnswer();
if (undefined !== userAnswer)
{
var hintDiv = _("hint");
if (true === userAnswer)
{
hintDiv.innerHTML = "<p id='correct'>OK</p>";
}
else if (false === userAnswer)
{
hintDiv.innerHTML = "<p id='wrong'>WRONG</p>";
}
$('input[name="choices"]').attr('disabled', 'disabled');
_("submitButton").disabled = true;
hintDiv.innerHTML += questions[currentQuestionNr].hint;
hintDiv.innerHTML += "<br><button onclick='proceed()' class='smallBtn'>Next</button>";
}
}
function proceed() {
currentQuestionNr++;
if (currentQuestionNr >= totalQuestionNr)
{
printResult();
}
else
{
printQuestion();
}
}
function printResult() {
var result = _("question");
var percentage = Math.round((100 * correctAnswersCount)/totalQuestionNr);
result.innerHTML = "<h2>You got " + correctAnswersCount + " of " + totalQuestionNr + " questions correct </h2>";
result.innerHTML += "<p id='percentage'>" + percentage + "%</p> ";
result.innerHTML += '<div style="text-align:center"><button onclick="startQuiz(totalQuestionNr)" class="btn" value="Start">Start again</button></div>';
_("status").innerHTML = "";
_("hint").innerHTML = "";
currentQuestionNr = 0;
correctAnswersCount = 0;
}
function startQuizInternal(value) {
console.log("Questions.length: " + questions.length);
var maxQuestions = value > 0 ? value : questions.length;
totalQuestionNr = Math.min(maxQuestions, questions.length);
shuffle(questions);
printQuestion();
_("content").className = "quiz";
}
function startQuiz(value) {
if (questions.length === 0)
{
loadQuestionsAndStartQuiz(value);
}
else
{
startQuizInternal(value);
}
}
function shuffle(array) {
var counter = array.length, temp, index;
while (counter > 0) {
index = Math.floor(Math.random() * counter);
counter--;
temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}