-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.js
More file actions
108 lines (95 loc) · 2.78 KB
/
action.js
File metadata and controls
108 lines (95 loc) · 2.78 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
var correctGuesses = [];
var answers = []
var quotesNumber = quotes.length;
var authors = [];
var quoteElement = document.getElementById("quote");
var selectedIndex;
var selectedQuote;
var answerElemets = [];
answerElemets.push(document.getElementById("answer-1"));
answerElemets.push(document.getElementById("answer-2"));
answerElemets.push(document.getElementById("answer-3"));
answerElemets.push(document.getElementById("answer-4"));
document.querySelectorAll('.answers').forEach(element => {
element.addEventListener('click', event => {
checkGuess(event);
})
});
function getRandomNumber(max) {
return Math.floor(Math.random() * max);
}
// get all authors from quotes array
function getAllAuthors() {
var authorsSet = new Set();
quotes.forEach(element => {
authorsSet.add(element.author);
});
authors = Array.from(authorsSet);
}
// get a new random quote that was not selected before
function getQuote() {
resetBackgroundColors();
while (true) {
var quoteIndex = getRandomNumber(quotesNumber);
if(!correctGuesses.includes(quoteIndex)) {
selectedQuote = quotes[quoteIndex];
console.log(selectedQuote.author);
answers.push(selectedQuote.author);
quoteElement.innerText = selectedQuote.quote;
selectedIndex = quoteIndex;
break;
}
}
getWrongAuthors();
setAnswers();
}
// function that gets 3 authors that will be used as wrong answers
function getWrongAuthors() {
var count = 0;
while (count < 3) {
authorIndex = getRandomNumber(authors.length);
if (!answers.includes(authors[authorIndex])) {
answers.push(authors[authorIndex]);
count++;
}
}
}
function setAnswers() {
for(var i = 0; i < 3; i++) {
var randomIndex = getRandomNumber(answers.length);
answerElemets[i].innerText = answers[randomIndex];
answers.splice(randomIndex, 1);
}
answerElemets[3].innerText = answers[0];
answers = [];
}
function checkGuess(event) {
console.log(event);
if(event.target.innerText == selectedQuote.author) {
event.target.style.setProperty('background-color', 'green');
correctGuesses.push(selectedIndex);
}
else {
event.target.style.setProperty('background-color', 'red');
highlightCorrectAnswer();
correctGuesses = [];
}
setTimeout(() => getQuote(), 2000);
}
function highlightCorrectAnswer(){
answerElemets.forEach(element => {
if (element.innerText == selectedQuote.author) {
element.style.setProperty('background-color', 'green');
}
});
}
function resetBackgroundColors(){
answerElemets.forEach(element => {
element.style.setProperty('background-color', 'initial');
});
}
function startGame(){
getAllAuthors();
getQuote();
}
startGame();