-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
177 lines (164 loc) · 3.98 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Created by The Young Programmer aka NemoNet
//Declaring Variables
var count=1, total=0, correct=0, wrong=0, lim=0;
var ans = "", question ="", input="", width= 220, t = '', n1, n2, n3, r1, r2, mode=0;
var opr = [];
//Hide Quiz Body At Start
$(document).ready(function() {
$("#quizbody").hide();
});
//Selecting Difficulty Level
function level(lvl) {
if (lvl<5) {
opr = ["+", "-", "*"];
if (lvl==1)
lim = 5;
else if (lvl==2)
lim = 10;
else if (lvl==3)
lim = 15;
else if (lvl==4)
lim = 30;
}
else if (lvl==5) {
lim = 50;
opr = ["+", "-", "*", "*"];
}
$("#diff").hide();
$("#quizbody").show();
if (mode==1) {
$("#donebtn").hide();
}
else {
$("#donebtn").show();
}
quiz();
}
//Generating Questions
function quiz() {
var len = opr.length;
n1 = Math.floor(Math.random() * lim);
n2 = Math.floor(Math.random() * lim);
n3 = Math.floor(Math.random() * lim);
r1 = opr[Math.floor(Math.random()*len)];
r2 = opr[Math.floor(Math.random()*len)];
question = n1+r1+n2+r2+n3;
ans = eval(question);
$("#question").html(question+" = ?");
t = setInterval(timeCheck, 120);
}
//Checking Answer
function check() {
var input = $("#answer").val();
if(input == ans) {
Swal.fire({
icon: 'success',
title: 'Correct',
text: ans + ' is correct.',
showConfirmButton: false,
timer: 1500
});
correct++;
$("#correct").html(correct);
}
else {
Swal.fire({
icon: 'error',
title: 'Wrong',
text: 'The answer was '+ans,
showConfirmButton: false,
timer: 1500
});
wrong++;
$("#wrong").html(wrong);
}
$("#answer").val('');
count++;
total++;
$("#no").html(count);
$("#total").html(total);
clearInterval(t);
width = 220;
bar.style.width = '200px';
quiz();
}
//Inserting Numbers
function ins(num) {
var chk = $("#answer").val().includes(".");
if ($("#answer").val() != '' && num == '-' || num == "." && chk)
{
//do nothing
}
else {
$("#answer").val($("#answer").val() + num);
if (mode==1) {
if ($("#answer").val() ==ans)
check();
}
}
}
//Timer
function timeCheck() {
var bar = document.getElementById("bar");
if(width == 0) {
clearInterval(t);
Swal.fire({
icon: 'warning',
title: 'Timeout',
text: 'The answer was '+ans,
showConfirmButton: false,
timer: 1500
});
wrong++;
$("#wrong").html(wrong);
quiz();
width = 220;
bar.style.width = '200px';
}
else {
width--;
bar.style.width = width+'px';
}
}
//Other Functions
$(function() {
//Quick mode
$("#mode").click(function() {
if (mode==0) {
$(this).html("Quick Mode (ON)");
mode=1;
}
else {
$(this).html("Quick Mode (OFF)");
mode=0;
}
});
//Reseting
$("#res").click(function() {
$("#diff").show();
$("#quizbody").hide();
count=1, total=0, correct=0, wrong=0, lim=0;
$("#correct").html(correct);
$("#wrong").html(wrong);
$("#total").html(total);
input.value = "";
clearInterval(t);
width = 220;
bar.style.width = '200px';
});
//Deleting a number
$("#del").click(function() {
var txt = $("#answer").val();
txt = txt.slice(0, -1);
$("#answer").val(txt);
});
//Explaining Quick mode
$("#ex").click(function() {
Swal.fire({
icon: 'info',
title: 'Quick Mode',
text: 'When Quick Mode is on, system will automatically detect the right answer',
showConfirmButton: true,
});
});
});