-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSonicGames.js
171 lines (142 loc) · 4.71 KB
/
SonicGames.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
(function() {
let questions = [{
question: "Who was the creator/lead programmer of Sonic?",
answerChoices: ["Yuji Naka", "Akira Toriyama", "Tomoya Ohtani", "Toei"],
rightAnswer: 0
}, {
question: "What was the proposed name for Sonic?",
answerChoices: ["The Blue Ball of Speed", "Mr.Needlemouse", "The Fast Weasel", "Naked Mole-Rat"],
rightAnswer: 1
}, {
question: "At what speed does Sonic sprint/run?",
answerChoices: ["550 MPH", "222 MPH", "1000 MPH", "767 MPH"],
rightAnswer: 3
}, {
question: "Who is Sonic's Archenemy?",
answerChoices: [ "Frieza", "Dr.Eggman", "Dr.CrazyFace", "Shadow The Hedgehog"],
rightAnswer: 1
}, {
question: "Where did Sonic grow up? ",
answerChoices: ["Emerald Hill", "Icy Fortress", "Water City", "Rock World"],
rightAnswer: 0
},
];
let questionUpticker = 0; //Tracks question number
let options = []; //Array containing user choices
let sonicQuiz = $('#sonicTrivia'); // sonicTrivia div object
// Show initial question
showNext();
// Click handler for the 'next' button
$('#sonicGo').on('click', function (e) {
e.preventDefault();
// Suspend click listener during fade animation
if(sonicQuiz.is('animation')) {
return false;
}
choose();
// If no user selection, progress is stopped
if (isNaN(options[questionUpticker])) {
alert('Choose an answer.');
} else {
questionUpticker++;
showNext();
}
});
// Click handler for the 'sonicBack' button
$('#sonicBack').on('click', function (e) {
e.preventDefault();
if(sonicQuiz.is('animation')) {
return false;
}
choose();
questionUpticker--;
showNext();
});
// Click handler for the 'sonicOver' button
$('#sonicOver').on('click', function (e) {
e.preventDefault();
if(sonicQuiz.is('animation')) {
return false;
}
questionUpticker = 0;
options = [];
showNext();
$('#sonicOver').hide();
});
// Animates buttons on hover
$('.button').on('cursorenter', function () {
$(this).addClass('active');
});
$('.button').on('cursorleave', function () {
$(this).removeClass('active');
});
// Creates and returns the div that contains the questions and the answer options
function createQuestionElement(index) {
let questionElement = $('<div>', {
id: 'question'
});
let headerOfSonic = $('<h3>Question ' + (index + 1) + ':</h3>');
questionElement.append(headerOfSonic);
let questionsOfSonic = $('<p>').append(questions[index].question);
questionElement.append(questionsOfSonic);
let radioButtonsOfSonic = createRadioInputs(index);
questionElement.append(radioButtonsOfSonic);
return questionElement;
}
// Creates a list of the answer choices as radio inputs
function createRadioInputs(index) {
let radioList = $('<ul>');
let sonicItem;
let input = '';
for (let i = 0; i < questions[index].answerChoices.length; i++) {
sonicItem = $('<li>');
input = '<input type="radio" name="answer" value=' + i + ' />';
input += questions[index].answerChoices[i];
sonicItem.append(input);
radioList.append(sonicItem);
}
return radioList;
}
// Reads the user selection and pushes the value to an array
function choose() {
options[questionUpticker] = +$('input[name="answer"]:checked').val();
}
// Displays next requested element
function showNext() {
sonicQuiz.fadeOut(function() {
$('#question').remove();
if(questionUpticker < questions.length){
let nextQuestion = createQuestionElement(questionUpticker);
sonicQuiz.append(nextQuestion).fadeIn();
if (!(isNaN(options[questionUpticker]))) {
$('input[value='+options[questionUpticker]+']').prop('checked', true);
}
// Controls display of 'sonicBack' button
if(questionUpticker === 1){
$('#sonicBack').show();
} else if(questionUpticker === 0){
$('#sonicBack').hide();
$('#sonicGo').show();
}
}else {
let pointElem = showPoints();
sonicQuiz.append(pointElem).fadeIn();
$('#sonicGo').hide();
$('#sonicBack').hide();
$('#sonicOver').show();
}
});
}
// Adds the points and returns a paragraph element to be displayed
function showPoints() {
let score = $('<p>',{id: 'question'});
let numCorrect = 0;
for (let i = 0; i < options.length; i++) {
if (options[i] === questions[i].rightAnswer) {
numCorrect++;
}
}
score.append('You have gathered ' + numCorrect + ' rings out of ' + questions.length);
return score;
}
})();