forked from Zwarzen/quizApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
155 lines (128 loc) · 4.55 KB
/
game.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
const question = document.getElementById('question');
const choices = Array.from(document.getElementsByClassName('choice-text'));
const progressText = document.getElementById('progressText');
const scoreText = document.getElementById('score');
const progressBarFull = document.getElementById('progressBarFull');
const loader = document.getElementById('loader');
const game = document.getElementById('game');
let currentQuestion = {};
let acceptingAnswers = false;
let score = 0;
let questionCounter = 0;
let availableQuesions = [];
let questions = [];
//Ambil data pertanyaan di JSON
fetch('https://raw.githubusercontent.com/Zwarzen/questions/main/questions.json')
.then(res=>res.json())
.then((resp)=>{
questions.push(...resp);
startGame()
//cek
console.log(resp)
console.log(questions)
console.log(availableQuesions)
})
.catch(err=> console.log(err))
//menggunakan json external
// setTimeout(()=>{
// console.log(externalQuestion);
// questions.push(...externalQuestion);
// startGame();
// //cek
// console.log(questions)
// console.log(availableQuesions)
// },10)
//CONSTANTS banyak pertanyaan
const CORRECT_BONUS = 20;
const MAX_QUESTIONS = 5;
startGame = () => {
questionCounter = 0;
score = 0;
availableQuesions = [...questions];
getNewQuestion();
game.classList.remove('hidden');
loader.classList.add('hidden');
};
//Progress Bar dan Question Index
getNewQuestion = () => {
if (availableQuesions.length === 0 || questionCounter >= MAX_QUESTIONS) {
localStorage.setItem('mostRecentScore', score); //set score saat ini ke local storage, untuk dioper kehalaman berikutnya
//go to the end page
return window.location.assign('./end.html');
}
questionCounter++;
progressText.innerText = `Pertanyaan ${questionCounter}/${MAX_QUESTIONS}`;
//Update the progress bar
progressBarFull.style.width = `${(questionCounter / MAX_QUESTIONS) * 100}%`;
const questionIndex = Math.floor(Math.random() * availableQuesions.length);
currentQuestion = availableQuesions[questionIndex];
question.innerHTML = currentQuestion.question;
if(currentQuestion.is_image){ //handle bila pertanyaan adalah gambar
choices.forEach((choice) => {
const number = choice.dataset['number'];
choice.innerHTML =
`<div style="display : flex; justify-content: center">
<img src="${currentQuestion['choice' + number]}" style="max-height: 100px;" />
</div>`;
});
}else{ //hadle bila pertanyaan adalah text
choices.forEach((choice) => {
const number = choice.dataset['number'];
choice.innerHTML = currentQuestion['choice' + number];
});
}
availableQuesions.splice(questionIndex, 1);
acceptingAnswers = true;
};
//Choice Benar/Salah
choices.forEach((choice) => {
choice.addEventListener('click', (e) => {
if (!acceptingAnswers) return;
acceptingAnswers = false;
let selectedChoice; // <<-- element yang diklik
//cek percabangan tergantungn element yg diklik
if(e.target.tagName == "DIV"){
selectedChoice = e.target.parentElement;
}else if(e.target.tagName == "IMG"){
selectedChoice = e.target.parentElement.parentElement;
}else{
selectedChoice = e.target;
}
const selectedAnswer = selectedChoice.dataset['number'];
const classToApply =
selectedAnswer == currentQuestion.answer ? 'correct' : 'incorrect';
if (classToApply === 'correct') {
incrementScore(CORRECT_BONUS);
}
selectedChoice.parentElement.classList.add(classToApply);
setTimeout(() => {
selectedChoice.parentElement.classList.remove(classToApply);
getNewQuestion();
}, 1000);
});
});
//Counter Score
incrementScore = (num) => {
score += num;
scoreText.innerText = score;
};
//Timer
function timerGame(){
let interval = 5; //5 detik
progressBar.style.width = "100%";
timeSpan.innerHTML = "5s"
timeInter = setInterval (() => {
interval--;
let progressWidth = interval / 5 * 100
if(interval >0){
progressBar.style.width = progressWidth + "%"
timeSpan.innerHTML = interval + "s"
checkColors(progressWidth)
}else{
clearInterval(timeInter)
progressBar.style.width = "0%";
timeSpan.innerHTML = "Game Over";
getNewQuestion();
}
},1000);
}