-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
108 lines (98 loc) · 3.2 KB
/
Copy pathgame.js
File metadata and controls
108 lines (98 loc) · 3.2 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
const question = document.getElementById("question");
const choices = Array.from(document.getElementsByClassName("choice-text"));
const ProgressText = document.getElementById("progressText");
const scoreText = document.getElementById("score");
const loader = document.getElementById("loader");
const progressBarFull = document.getElementById("progressBarFull");
let currentQuestion = {};
let acceptingAnswers = false;
let score = 0;
let questionCounter = 0;
let availableQuestions = [];
let questions = [];
fetch("https://opentdb.com/api.php?amount=10&category=18&difficulty=medium")
.then((res) => {
return res.json();
})
.then((loadedQuestions) => {
console.log(loadedQuestions.results);
questions = loadedQuestions.results.map((loadedQuestion) => {
const formattedQuestion = {
question: loadedQuestion.question,
};
const answerChoice = [...loadedQuestion.incorrect_answers];
formattedQuestion.answer = Math.floor(Math.random() * 3) + 1;
answerChoice.splice(
formattedQuestion.answer - 1,
0,
loadedQuestion.correct_answer
);
answerChoice.forEach((choice, index) => {
formattedQuestion["choice" + (index + 1)] = choice;
});
return formattedQuestion;
});
setTimeout(() => {
game.classList.remove("hidden");
loader.classList.add("hidden");
}, 5000);
startGame();
});
console.log(questions);
//CONSTANTS
const CORRECT_BONUS = 4;
const NEGATIVE_MARKS = 1;
const MAX_QUESTIONS = 10;
startGame = () => {
questionCounter = 0;
score = 0;
availableQuestions = [...questions];
console.log(availableQuestions);
getNewQuestion();
};
getNewQuestion = () => {
if (questionCounter >= MAX_QUESTIONS || availableQuestions.length == 0) {
console.log("saving score in ls");
localStorage.setItem("mostRecentScore", score);
window.location.assign("/end.html");
}
questionCounter++;
ProgressText.innerText = `Question ${questionCounter}/${MAX_QUESTIONS}`;
progressBarFull.style.width = `${
((questionCounter - 1) / MAX_QUESTIONS) * 100
}%`;
const questionIndex = Math.floor(Math.random() * availableQuestions.length);
currentQuestion = availableQuestions[questionIndex];
question.innerText = currentQuestion.question;
choices.forEach((choice) => {
const number = choice.dataset["number"];
choice.innerText = currentQuestion["choice" + number];
});
availableQuestions.splice(questionIndex, 1);
acceptingAnswers = true;
};
choices.forEach((choice) => {
choice.addEventListener("click", (e) => {
if (!acceptingAnswers) {
return;
}
acceptingAnswers = false;
console.log(e);
const selectedChoice = e.target;
const selectedAnswer = selectedChoice.dataset["number"];
console.log(selectedAnswer, currentQuestion.answer);
const classToApply =
selectedAnswer == currentQuestion.answer ? "correct" : "incorrect";
selectedChoice.classList.add(classToApply);
if (classToApply == "correct") {
score = score + CORRECT_BONUS;
} else {
score = score - NEGATIVE_MARKS;
}
scoreText.innerText = score;
setTimeout(() => {
selectedChoice.classList.remove(classToApply);
getNewQuestion();
}, 500);
});
});