-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuizgame.js
141 lines (137 loc) · 4.5 KB
/
Quizgame.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
var quangdb = new Dictionary();
quangdb.init("word_define.txt");
var timesChallenge = 3;
function check(x, ans) {
if (x== 0 && ans =="A") return true;
else if (x == 1 && ans == "B") return true;
else if (x == 2 && ans == "C") return true;
else if (x == 3 && ans == "D") return true;
else return false;
}
var arrqq = [];
var times = 0;
var x;
var arr = [];
var maximum =0;
var count = 0;
function showUserInput() {
document.getElementById("user-reply").style.display = "block";
document.getElementById("submit-button").style.display = "block";
}
function resetPractice(){
document.getElementById("practice-container").textContent = '';
}
function createQuestion(){
arr = [];
for(var i = 0 ; i < 4; i++){
if (modePractice == 3){
arr.push(arrqq[rand(0,arrqq.length)]);
}
else
{
arr.push(quangdb.aWordStartWith(String.fromCharCode(rand(97,122))));
}
}
x = rand(0,3);
arrqq.push(arr[x]);
addDivToContainer("Mean: " + quangdb.getMean(arr[x]) + " ?","practice-container","quesion");
addDivToContainer("A: " + arr[0],"practice-container","choice");
addDivToContainer("B: " + arr[1],"practice-container","choice");
addDivToContainer("C: " + arr[2],"practice-container","choice");
addDivToContainer("D: " + arr[3],"practice-container","choice");
}
function setTime(){
times = parseInt (window.prompt("Ban muon choi bao nhieu lan ?"));
}
var modePractice = 1;
document.getElementById("user-reply").addEventListener("keydown",function(event){
if (event.key == "Enter"){
console.log(modePractice);
var ans = document.getElementById("user-reply").value;
if (modePractice == 1){
if (times > 0)
{
if (ans != ""){
times--;
printResult(check(x,ans));
createQuestion();
}
}
if (times == 0) {
window.alert("End game!")
resetPractice();
learn();
}
}
else
if (modePractice == 2){
if (timesChallenge > 0){
if (ans!= ""){
var temp = check(x,ans);
count += temp;
timesChallenge += temp-1;
console.log(count,timesChallenge);
printResult(temp);
createQuestion();
}
}
if (timesChallenge == 0)
{
if (count > maximum) {
window.alert("You lost ! New record is " + count);
maximum = count;
}
else window.alert("You lost ! Your score is " + count);
resetPractice();
}
}
else if (modePractice ==3) {
if (times > 0)
{
if (ans != ""){
times--;
printResult(check(x,ans));
createQuestion();
}
}
if (times == 0){
window.alert("End game!");
resetPractice();}
}
document.getElementById("user-reply").value = '';
}
});
function printResult(check){
if (check) {
addDivToContainer(arr[x] + " is correct", "practice-container", "correct");
}
else
{
addDivToContainer(arr[x] + " is wrong", "practice-container", "wrong");
}
}
function test(){
showUserInput();
modePractice = 3;
resetPractice();
setTime();
createQuestion();
}
function challenge(){
showUserInput();
count =0;
modePractice = 2;
timesChallenge = 3;
window.alert("Ban duoc sai toi da " + timesChallenge);
createQuestion();
}
function learn(){
showUserInput();
modePractice = 1;
resetPractice();
setTime();
createQuestion();
}
//1: learn
//2: challenge
//3: test