-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
289 lines (263 loc) · 8.34 KB
/
index.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
const start_btn = document.querySelector(".start_btn button");
const leaderboard = document.querySelector(".leaderboard button");
const info_box = document.querySelector(".instructions");
const exit_btn = info_box.querySelector(".buttons .quit");
const continue_btn = info_box.querySelector(".buttons .restart");
const quiz_box = document.querySelector(".math-quiz");
const option_list = document.querySelector(".option_list");
const timeCount = quiz_box.querySelector(".timer .timer_sec");
const timeLine = quiz_box.querySelector("header .time_line");
const timeOff = quiz_box.querySelector("header .time_text");
const next_btn = quiz_box.querySelector(".next_btn");
const result_box = document.querySelector(".result_box");
const send_result = result_box.querySelector(".buttons .restart");
const restart_quiz = result_box.querySelector(".buttons .quit");
const name_box = document.querySelector(".name_box");
const name_input = document.querySelector("#container .name");
const name_btn = document.querySelector("#container .name_btn");
//start of jQuery function
//Keyboard events
// $(document).ready(function(){
// $(document).keypress(function(event) {
// if (event.keyCode === 49) { //when 1 is pressed
// document.getElementById("option1").click()//clicks 1rd option
// } else if(event.keyCode === 50){ //when 2 is pressed
// document.getElementById("option2").click()//clicks 2rd option
// } else if(event.keyCode === 51){ //when 3 is pressed
// document.getElementById("option3").click()//clicks 3rd option
// } else if(event.keyCode === 52){ //when 4 is pressed
// document.getElementById("option4").click()//clicks 4th option
// } else if(event.which == 13){
// return false;
// }
// })
// }) /*failed because when the spacebar is entered the timeline misbehaves function misbehaves expect that everthing is alright in this function*/
//end of jQuery functions
name_btn.onclick = () => {
checkName();
};
function checkName() {
var letters = /^[A-Z\sa-z]+$/; //allows only upper n lower alphabets with space
name_input.value = name_input.value.match(letters);
if (document.querySelector("#container .name").value == "") {
name_input.value = "";
name_input.placeholder = "kamu nggak punya nama?";
} else {
name_input.placeholder = "Siapa nama kamu?";
name_box.classList.remove("activeName");
info_box.classList.add("activeInfo");
username = name_input.value;
console.log("Nama kamu " + username);
}
}
// If Start quiz Button Clicked
start_btn.onclick = () => {
name_box.classList.add("activeName");
};
leaderboard.onclick = () => {
document.getElementById("leaderboard_link").click();
};
//If Exit Button is Clicked
exit_btn.onclick = () => {
info_box.classList.remove("activeInfo");
};
//If Continue Button Clicked
continue_btn.onclick = () => {
info_box.classList.remove("activeInfo");
quiz_box.classList.add("activeQuiz");
createQuestions();
queCounter(0);
startTimer(timerValue);
startTimerLine(0);
};
//On clicking it shows result box
next_btn.onclick = () => {
showResultBox();
changeText();
};
let que_count = 0;
let counter;
let counterLine;
let timerValue = 15; // Set time
let widthValue = 0;
let userScore = 0;
let correctAns;
let username;
// for restart button
restart_quiz.onclick = () => {
result_box.classList.remove("activeResult");
que_count = 0;
widthValue = 0;
userScore = 0;
timeOff.textContent = "Time Left";
next_btn.style.display = "none";
clearInterval(counterLine);
clearInterval(counter);
let alloptions = option_list.children.length;
for (let i = 0; i < alloptions; i++) {
if (option_list.children[i].textContent == correctAns) {
option_list.children[i].classList.remove("correct");
}
option_list.children[i].classList.remove("disabled", "wrong");
}
};
function randomNumber() {
return Math.floor(Math.random() * 20 + 1);
}
function randomAnswer(answer) {
return Math.floor(Math.random() * answer + Math.floor(answer / 2));
}
function createQuestions() {
var num1 = randomNumber();
var num2 = randomNumber();
document.getElementById("num1").innerText = num1;
document.getElementById("num2").innerText = num2;
answer = num1 * num2;
correctAns = answer;
var options = [];
var a = Math.floor(Math.random() * 4);
options[a] = answer;
for (var i = 0; i < 4; i++) {
if (a == i) {
continue;
}
options[i] = randomAnswer(answer);
}
for (var i = 0; i < 4; i++) {
document.querySelectorAll(".option")[i].innerText = options[i];
document
.querySelectorAll(".option")
[i].setAttribute("onclick", "optionSelected(this)");
}
}
function optionSelected(answer) {
clearInterval(counterLine);
clearInterval(counter);
startTimer(15);
startTimerLine(0);
let userAns = answer.textContent;
let alloptions = option_list.children.length;
if (userAns == correctAns) {
userScore += 1;
createQuestions();
que_count++;
queCounter(que_count);
console.log("Answer is Correct");
} else {
clearInterval(counterLine);
clearInterval(counter);
answer.classList.add("wrong");
console.log("Answer is wrong");
for (let i = 0; i < alloptions; i++) {
if (option_list.children[i].textContent == correctAns) {
option_list.children[i].setAttribute("class", "option correct");
}
}
for (let i = 0; i < alloptions; i++) {
option_list.children[i].classList.add("disabled");
}
next_btn.style.display = "block";
console.log("Questions completed");
}
}
function startTimer(time) {
counter = setInterval(timer, 1000);
function timer() {
timeCount.textContent = time;
time--;
if (time < 9) {
let addZero = timeCount.textContent;
timeCount.textContent = "0" + addZero;
}
if (time < 0) {
clearInterval(counter);
timeCount.textContent = "00";
timeOff.textContent = "Time Out";
let alloptions = option_list.children.length;
for (let i = 0; i < alloptions; i++) {
if (option_list.children[i].textContent == correctAns) {
option_list.children[i].setAttribute("class", "option correct");
}
}
for (let i = 0; i < alloptions; i++) {
option_list.children[i].classList.add("disabled");
}
next_btn.style.display = "block";
}
}
}
function startTimerLine(time) {
counterLine = setInterval(timer, 50);
function timer() {
time += 0.3125;
if (time > 100) {
clearInterval(counterLine);
} else {
timeLine.style.width = time + "%";
}
}
}
function showResultBox() {
info_box.classList.remove("activeInfo"); //hide the info box
quiz_box.classList.remove("activeQuiz"); //hide the quiz box
result_box.classList.add("activeResult"); //show the result box
const scoreText = result_box.querySelector(".score_text");
let scoreTag = "<span>kamu mendapatkan<p>" + userScore + "</p> Poin</span>";
scoreText.innerHTML = scoreTag;
}
function queCounter(index) {
const bottom_ques_counter = quiz_box.querySelector(".point_no");
const totalQuesCountTag = index;
bottom_ques_counter.innerHTML = totalQuesCountTag;
}
function changeText() {
var changetext = document.querySelector(".complete_text");
changetext.innerHTML =
"Selamat " + username + "! kamu berhasil menyelesaikan quiz";
}
send_result.onclick = () => {
encript();
};
function randomAlpha() {
return String.fromCharCode(64 + parseInt(Math.floor(Math.random() * 26 + 1)));
}
function encript() {
var encriptScore = "" + (userScore + 1527);
var encript = encriptScore.split("");
arr = [
"w",
"P",
"f",
"R",
"i",
"Y",
"A",
"a",
"g",
"N",
"q",
"s",
"U",
"m",
"T",
];
for (var i = 0; i < encript.length; i++) {
encript[i] = arr[encript[i]];
}
var r = randomAlpha();
var s = randomAlpha();
var q = randomAlpha();
link =
"https://api.whatsapp.com/send?phone=6289606032177text=Name%3A%20" +
username +
"%0Acode%3A%20*" +
r +
encript.join("") +
s +
q +
"*%0AScore%3A%2" +
userScore;
var l = document.getElementById("link");
l.setAttribute("href", link);
l.click();
}