-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
65 lines (52 loc) · 1.6 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
var countries = require('./country-flags/countries.json');
var countryList = Object.keys(countries);
var flagContainer = document.getElementById('flagContainer');
var countryName = document.getElementById('countryName');
var nextButton = document.getElementById('next');
var cheatButton = document.getElementById('cheat');
var input = document.getElementById('countryInput');
var country;
function getCountry() {
var i = random(countryList.length);
var initials = countryList[i];
country = {
name: countries[initials],
initials: initials
};
}
function refreshCountry() {
input.value = '';
getCountry();
cheatButton.classList.remove('hidden');
nextButton.classList.add('hidden');
countryName.classList.add('invisible');
countryName.textContent = country.name[0];
flagContainer.src = './country-flags/svg/' + country.initials.toLowerCase() + '.svg';
input.focus();
}
function cheat() {
countryName.classList.remove('invisible');
cheatButton.classList.add('hidden');
nextButton.classList.remove('hidden');
}
function random(max) {
return Math.floor(Math.random() * max);
}
function normalize(name) {
return name.toLowerCase().replace(/'\./, "").replace(/-/, " ");
}
function main() {
refreshCountry();
nextButton.addEventListener('click', refreshCountry);
cheatButton.addEventListener('click', cheat);
document.addEventListener('keydown', function (event) {
if (event.which === 13) {cheat();}
});
input.addEventListener('input', function () {
var answer = normalize(input.value);
if (country.name.includes(answer)) {
refreshCountry();
}
});
}
main();