-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
85 lines (74 loc) · 2.95 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
const mapsAndNames = [];
const apiUrl = 'https://restcountries.com/v3.1/all';
fetch(apiUrl)
.then(response => {
if(!response.ok){
throw new Error("Could not fetch");
}
return response.json();
})
.then(data => {
data.forEach(item => {
const countryInfo = {
commonName: item.name.common,
officialName: item.name.official,
flag: item.flags.png
};
mapsAndNames.push(countryInfo);
});
console.log(mapsAndNames);
})
.catch(error => {
console.error('Error fetching data:', error);
});
var x = 0;
var y = 0;
var z = 0;
document.addEventListener("DOMContentLoaded", function() {
const scoreChange = document.getElementById("num");
const defaultImage = document.getElementById("image");
const changeImageButton = document.getElementById("guessButton");
const answerKey = document.getElementById("answer");
const changeImageButtonAfterSkip = document.getElementById("skipButton");
let randomCountry;
changeImageButton.addEventListener("click", function() {
if (y < 1) {
randomCountry = mapsAndNames[Math.floor(Math.random() * mapsAndNames.length)];
const randomMap = randomCountry.flag;
defaultImage.src = randomMap;
y++;
}
document.getElementById("num").textContent = x;
const userInput = document.getElementById('input').value;
const tryAgain = document.getElementById("tryAgainLabel");
z++;
if (userInput.toLowerCase() == randomCountry.commonName.toLowerCase() ||
userInput.toLowerCase() == randomCountry.officialName.toLowerCase()) {
document.getElementById('input').value = "";
document.getElementById("num").textContent = ++x;
randomCountry = mapsAndNames[Math.floor(Math.random() * mapsAndNames.length)];
const randomMap = randomCountry.flag;
defaultImage.src = randomMap;
tryAgain.style.visibility = 'hidden';
}
else {
if (z>1) {
tryAgain.style.visibility = 'visible';
}
}
document.getElementById("answerbox").textContent = randomCountry.commonName;
});
changeImageButtonAfterSkip.addEventListener("click", function() {
const userInput = document.getElementById('input').value;
const tryAgain = document.getElementById("tryAgainLabel");
tryAgain.style.visibility = 'hidden';
randomCountry = mapsAndNames[Math.floor(Math.random() * mapsAndNames.length)];
const randomMap = randomCountry.flag;
defaultImage.src = randomMap;
y++;
document.getElementById("num").textContent = x;
document.getElementById("num").textContent = --x;
document.getElementById('input').value = "";
document.getElementById("answerbox").textContent = randomCountry.commonName;
});
});