-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
195 lines (160 loc) · 5.76 KB
/
main.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
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
import numberSeparator from "number-separator";
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyBB09h45wFutyzssyMzHwdLdhUo1ZSpAVQ",
authDomain: "countries-game-fcc4f.firebaseapp.com",
projectId: "countries-game-fcc4f",
storageBucket: "countries-game-fcc4f.appspot.com",
messagingSenderId: "448314381112",
appId: "1:448314381112:web:c2175b7aa2c404c20667cd"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const restartBtn = document.getElementById("restartBtn");
const firstCountryImg = document.getElementById("firstCountryImg");
const secondCountryImg = document.getElementById("secondCountryImg");
const countryOneName = document.getElementById('countryOneName')
const countryTwoName = document.getElementById('countryTwoName')
const countryOneValue = document.getElementById('countryOneValue')
const questionText = document.querySelectorAll('.questionText')
const moreBtn = document.getElementById('moreBtn')
const lessBtn = document.getElementById('lessBtn')
const currentScoreText = document.getElementById('currentScoreText')
const currentScoreValue = document.getElementById('currentScoreValue')
const maxScoreValue = document.getElementById('maxScoreValue')
const currentScoreContainer = document.getElementById('currentScoreContainer')
const changeVisibilityRestartBtn = () => {
restartBtn.style.display = (restartBtn.style.display == 'none')?'block':'none'
}
const createGame = () => {
let currentScore = 0
let maxScore = 0
let gameOver = false
let currentQuestionText = 'population'
let countryOne = []
let countryTwo = []
let countryThree = []
let countryOneQuantity
let countryTwoQuantity
const getCountries = async () => {
moreBtn.addEventListener('click', game.playRound)
lessBtn.addEventListener('click', game.playRound)
const start = Date.now();
countryOne = await getARandomCountry()
countryTwo = await getARandomCountry()
countryThree = await getARandomCountry()
showCountries()
const end = Date.now();
console.log(`Execution time: ${end - start} ms`);
}
const showCountries = () => {
firstCountryImg.src = `${countryOne.flag}`;
secondCountryImg.src = `${countryTwo.flag}`
countryOneName.innerText = countryOne.name
countryTwoName.innerText = countryTwo.name
choseQuestion()
}
const changeFlags = async () => {
countryOne = countryTwo
countryTwo = countryThree
showCountries()
countryThree = await getARandomCountry()
}
const choseQuestion = () => {
const possibleQuestionTexts = ['area', 'population']
let questionTextIndex = Math.round(Math.random() * (possibleQuestionTexts.length - 1))
currentQuestionText = possibleQuestionTexts[questionTextIndex]
if (currentQuestionText == 'population'){
countryOneValue.innerText = numberSeparator(countryOne.population)
questionText.forEach(text => text.innerText = 'habitants')
}
if (currentQuestionText == 'area'){
countryOneValue.innerText = `${countryOne.area} km2`
questionText.forEach(text => text.innerText = 'territory')
}
}
const playRound = (e) => {
const answer = e.target.innerText
let isCorrect
if(currentQuestionText == 'population'){
countryOneQuantity = countryOne.population
countryTwoQuantity = countryTwo.population
}
if(currentQuestionText == 'area'){
countryOneQuantity = countryOne.area
countryTwoQuantity = countryTwo.area
}
if(answer == 'More'){
isCorrect = countryTwoQuantity >= countryOneQuantity
}
if(answer == 'Less'){
isCorrect = countryTwoQuantity <= countryOneQuantity
}
if (isCorrect){
changeFlags()
changeScore()
}
if (!isCorrect){
gameOver = true
maxScore = currentScore>maxScore? currentScore: maxScore
moreBtn.addEventListener('click', game.playRound)
lessBtn.removeEventListener('click', game.playRound)
currentScoreText.innerText = 'Game Over, Final Score: '
currentScoreContainer.style.color = 'red'
changeVisibilityRestartBtn()
}
}
const changeScore = () => {
currentScore++
currentScoreValue.innerText = currentScore
}
const restartGame = () => {
maxScoreValue.innerText = maxScore
currentScore = 0
currentScoreValue.innerText = currentScore
currentScoreText.innerText = 'Current score: '
currentScoreContainer.style.color = 'blue'
getCountries()
changeVisibilityRestartBtn()
}
return {
getCountries,
restartGame,
changeFlags,
playRound
}
}
const game = createGame()
document.addEventListener('DOMContentLoaded', game.getCountries)
moreBtn.addEventListener('click', game.playRound)
lessBtn.addEventListener('click', game.playRound)
async function fetchCountries() {
const countriesPopulation = [];
const response = await fetch("https://restcountries.com/v3.1/all");
const responseData = await response.json();
await responseData.forEach((el) => {
const country = {
name: el.name.common,
population: el.population,
area: el.area,
flag: el.flags['svg'],
alpha3Code: el.cca3,
};
countriesPopulation.push(country);
});
return countriesPopulation;
}
async function getARandomCountry() {
const countries = await fetchCountries();
const numberOfCountries = countries.length - 1;
const randomNumber = Math.ceil(Math.random() * numberOfCountries);
return countries[randomNumber];
}
restartBtn.addEventListener('click', game.restartGame)
const firebaseAppConfig = getFirebaseConfig();
initializeApp(firebaseAppConfig);
//step seven