-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex-1.js
More file actions
209 lines (185 loc) · 6.04 KB
/
index-1.js
File metadata and controls
209 lines (185 loc) · 6.04 KB
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
const select = document.querySelector("#language"); // select element
const body = document.querySelector("body");
const container = document.querySelector(".container");
const btn_2 = document.querySelector(".btn_2");
const intro = document.querySelector(".intro");
const quiz = document.querySelector(".quiz");
const introMessage = document.querySelector(".introMessage");
const quizImage = document.querySelector(".quizImage img");
const multipleChoice = [...document.querySelector(".multipleChoice").children];
const quizLength = multipleChoice.length;
const circleContainer = document.querySelector(".circleContainer")
const circleBar = 360 / quizLength; // if five question value is 72degree;
const finalScore = document.querySelector(".finalScore");
const bubble = document.querySelector(".bubble");
const gif = document.querySelector(".gif");
const coloredElements = document.querySelector(".coloredElements");
const timer = document.querySelector(".timer");
const questionAnswered = document.querySelector(".questionAnswered");
const btn_1 = document.querySelector(".btn_1");
const landingPage = document.querySelector(".landingPage");
const input = document.querySelector("input");
let [ratio, counter, count, clock, score, timeUp] = [0, 0, 0, 0, 0, 10];
// let score = count/quizLength;
console.log(input.value)
let japaneseAnimeJapanese = [
{
name: "ドラゴンボール",
picture: "https://dbgbh.bn-ent.net/assets/img/news/news_thumb_kv.png"
},
{
name: "進撃の巨人",
picture: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSJYjy_bS8t3ScWyG7q94fIltnar3ChaOHmGA&usqp=CAU"
},
{
name: "ナルト",
picture: "https://res.cloudinary.com/jerrick/image/upload/v1616592065/605b3cc118e784001e22da0d.jpg"
},
{
name: "鬼滅の刃",
picture: "https://cdn.vox-cdn.com/thumbor/gcVHhhZ4VwVswvbDPvI-RfQ7ECQ=/1400x1050/filters:format(png)/cdn.vox-cdn.com/uploads/chorus_asset/file/19721018/Tanjiro__Demon_Slayer_.png"
},
{
name: "攻殻機動隊",
picture: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS8VBbI5HMki5cmjP_Gq0TdyA6VZn_0_fmkhg&usqp=CAU"
}
]
let japaneseAnimeEnglish = [
{
name: "Dragon ball",
picture: "https://dbgbh.bn-ent.net/assets/img/news/news_thumb_kv.png"
},
{
name: "Attack On Titans",
picture: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSJYjy_bS8t3ScWyG7q94fIltnar3ChaOHmGA&usqp=CAU"
},
{
name: "naruto",
picture: "https://res.cloudinary.com/jerrick/image/upload/v1616592065/605b3cc118e784001e22da0d.jpg"
},
{
name: "Demon Slayer",
picture: "https://cdn.vox-cdn.com/thumbor/gcVHhhZ4VwVswvbDPvI-RfQ7ECQ=/1400x1050/filters:format(png)/cdn.vox-cdn.com/uploads/chorus_asset/file/19721018/Tanjiro__Demon_Slayer_.png"
},
{
name: "Ghost in the shell",
picture: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS8VBbI5HMki5cmjP_Gq0TdyA6VZn_0_fmkhg&usqp=CAU"
}
]
// export {japaneseAnimeJapanese, japaneseAnimeEnglish};
// import {japaneseAnimeJapanese, japaneseAnimeEnglish} from "./index-2.js"
const ranking = JSON.parse(localStorage.getItem("userObject")) || [];
function userRanking() {
ranking.push({
userName: input.value,
userScore: (score / quizLength) * 100,
})
localStorage.setItem("userObject", JSON.stringify(ranking))
}
btn_1.addEventListener("click", slideFunction);
btn_2.addEventListener("click", startQuiz);
select.addEventListener("change", displayButton);
function slideFunction() {
landingPage.classList.toggle("slides");
}
function quizClock() {
timer.innerHTML = timeUp;
circleContainer.style.background = `conic-gradient(red ${ratio*36}deg, blue ${ratio*36}deg)`
if(timeUp > 0) {
timeUp--;
ratio++;
} else {
timeUp = 10;
ratio = 0;
wrongAnswer()
update()
}
}
function userChoice(e) {
for(var i = 0; i < multipleChoice.length; i++) {
multipleChoice[i].addEventListener("click", checkUserAnswer)
}
}
function checkUserAnswer(e) {
let answer = e.target;
if(answer.innerHTML === japaneseAnimeEnglish[counter].name) {
correctAnswer();
count++;
score++;
} else {
wrongAnswer();
}
update();
ratio = 0;
timeUp = 10;
}
function wrongAnswer() {
document.getElementById(counter).style.background = "red";
}
function correctAnswer() {
document.getElementById(counter).style.background = "green";
}
function update() {
if(counter < quizLength - 1) {
counter++;
displayUniqueChoices();
trackQuestion();
} else {
displayFinalScore();
}
}
function trackQuestion() {
if(counter < quizLength) {
questionAnswered.innerHTML = `question ${counter+1} of ${quizLength}`;
}
}
//
function displayFinalScore() {
quiz.style.display = "none";
finalScore.style.display = "flex";
bubble.innerHTML = `<h1>Well done ${input.value} Your final score is ${(score/quizLength)*100}%</h1>`
gif.innerHTML = `<img src="images/dragon-ball-z-goku.gif" alt="">`
document.body.style.backgroundColor = "#368dda";
userRanking()
}
function randomNumbers() {
return Math.floor(Math.random()*quizLength);
}
function generateRandomNumbers() {
const uniqueNumbers = new Set();
while (uniqueNumbers.size < quizLength) {
uniqueNumbers.add(randomNumbers())
}
return [...uniqueNumbers]
}
function displayUniqueChoices() {
quizImage.src = japaneseAnimeEnglish[counter].picture;
const arrayUniqueDigits = generateRandomNumbers();
multipleChoice.forEach((choice, i) => {
choice.innerHTML = japaneseAnimeEnglish[arrayUniqueDigits[i]].name
})
}
function displayRoundElements() {
for(var i = 0; i < quizLength; i++) {
coloredElements.innerHTML += `<div id=${i} class="roundBoxes"></div>`
}
}
function displayButton() {
btn_2.style.display = "block";
introMessage.style.display = "none";
}
function startQuiz() {
quiz.style.display = "flex";
container.style.display = "none";
intro.style.display = "none";
displayUniqueChoices();
userChoice();
displayRoundElements();
trackQuestion()
setInterval(quizClock, 1000);
}
body.addEventListener("click", function(e) {
if(input.value === "" && e.key !== "Enter") return;
input.style.display = "none";
select.style.display = "block";
})