-
Notifications
You must be signed in to change notification settings - Fork 2
/
round2.js
104 lines (91 loc) · 3.34 KB
/
round2.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
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
var qst = document.getElementById("round2");
var ans = document.getElementById("round-2");
var incorrect = document.getElementById("incorrect");
async function getDataFromFirestore() {
const db = firebase.firestore();
const user = await db.collection("user");
const userRef = user.doc(localStorage.getItem('userId'));
const userSnapshot = await userRef.get();
const userData = userSnapshot.data();
if (userData.round == 1) {
const querySnapshot = await db.collection("data2").get();
const dataArray = querySnapshot.docs.map((doc) => doc.data());
const questionAnswerId = getRandomInt(dataArray.length);
user.doc(localStorage.getItem('userId')).update({
round : 2
})
user.doc(localStorage.getItem('userId')).update({
r2q : dataArray[questionAnswerId]["question"],
r2a : dataArray[questionAnswerId]["answer"]
})
qst.innerHTML = dataArray[questionAnswerId]["question"];
document.getElementById("round-2").addEventListener("keyup", (e) => {
if (
e.target.value.toLowerCase() ==
dataArray[questionAnswerId]["answer"].toLowerCase()
) {
document.querySelector("#submit").style.display = "block";
} else {
document.querySelector("#submit").style.display = "none";
}
});
document.getElementById("submit").addEventListener("click", (e) => {
e.preventDefault();
var answer = document.getElementById("round-2").value;
if (
answer.toLowerCase() ==
dataArray[questionAnswerId]["answer"].toLowerCase()
) {
setTimeout(
3000,
alert("Congratulations, you have completed the second round")
);
window.location.replace("round2b.html");
} else {
incorrect.innerHTML = "Incorrect answer";
}
});
} else {
const userRef = user.doc(localStorage.getItem('userId'));
const userSnapshot = await userRef.get();
const userData = userSnapshot.data();
if (userData.round == 1) {
window.location.replace("round2.html");
} else if (userData.round == 3) {
window.location.replace("round3.html")
} else if (userData.round == 4) {
window.location.replace("round4.html");
}
qst.innerHTML = userData.r2q;
document.getElementById("round-2").addEventListener("keyup", (e) => {
if (
e.target.value.toLowerCase() ==
userData.r2a.toLowerCase()
) {
document.querySelector("#submit").style.display = "block";
} else {
document.querySelector("#submit").style.display = "none";
}
});
document.getElementById("submit").addEventListener("click", (e) => {
e.preventDefault();
var answer = document.getElementById("round-2").value;
if (
answer.toLowerCase() ==
userData.r2a.toLowerCase()
) {
setTimeout(
3000,
alert("Congratulations, you have completed the second round")
);
window.location.replace("round2b.html");
} else {
incorrect.innerHTML = "Incorrect answer";
}
});
}
}
getDataFromFirestore();