-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
184 lines (139 loc) · 5.24 KB
/
app.js
File metadata and controls
184 lines (139 loc) · 5.24 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
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
//Designated Pairs
const cityCountryPairs = 9;
let answers = [];
//UI variables
const finalSubmitButton = document.getElementById('finalSubmit');
const capitalTiles = document.querySelector('.capitalTiles');
const countryCards = document.querySelector('.countryCards');
const newCitiesButton = document.getElementById('newSet');
//Event Listeners
document.addEventListener('DOMContentLoaded', createGamePieces);
finalSubmitButton.addEventListener('click', showLoadingWheel);
newCitiesButton.addEventListener('click', createGamePieces);
document.querySelector('.playAgainButton').addEventListener('click', playAgain);
//HELPER FUNCTIONS
//Display Loading wheel
function showLoadingWheel(){
document.querySelector('.loading').style.display = "block";
setTimeout(checkAnswers, 3000);
}
//Hide Loading wheel and actually
//Check Answers
function checkAnswers(){
//Hide Loading wheel
document.querySelector('.loading').style.display = "none";
//Get Array of capital names that were placed
let userAnswers = [];
let answersCorrect = 0;
for (let index = 0; index < cityCountryPairs; index++) {
let userAnswerID = `.country${index}`;
userAnswers[index] = document.querySelector(userAnswerID).firstElementChild.textContent;
if (userAnswers[index] ==answers[index]){
answersCorrect++;
}
setHighlights(userAnswers[index] === answers[index], userAnswerID);
}
//Disable Buttons
disableButtonsAndTiles();
//alert user to game status
setHeaderMessage(answersCorrect);
}
//Restrict user Input to the Play Again Button Only
function disableButtonsAndTiles(){
newCitiesButton.disabled = true;
document.getElementById('submit').disabled = true;
let capitalTilesList = document.querySelectorAll('.capital');
console.log(capitalTilesList);
capitalTilesList.forEach( function(tile){
tile.draggable=false;
})
}
//Show user which answers they got correct
function setHighlights( isGreen, userAnswerID) {
cardBody = document.querySelector(userAnswerID).parentElement
cardBody.classList.add('border');
if (isGreen ){
cardBody.classList.add('border-success');
} else {
cardBody.classList.add('border-warning');
}
}
function setHeaderMessage(count){
let headerMessage = '';
let subMessage
const percent = count/cityCountryPairs;
if (percent < .25){
headerMessage = 'Couch Potato';
subMessage = 'Do you even know where you live?';
} else if (percent >= .25 && percent < .5){
headerMessage = 'Homebody';
subMessage = 'Don\'t get around much do you?';
} else if (percent >= .5 && percent < 1){
headerMessage = 'Tourist';
subMessage = 'You\'ve seen a few places';
} else {
headerMessage = 'Globetrotter';
subMessage = '\'Travel\' is your middle name';
}
document.querySelector('.heading').textContent = `Status: ${headerMessage}`;
document.querySelector('.submessage').textContent = subMessage;
//Show play again button
document.querySelector('.playAgain').style.display = "block";
}
//Refresh page for a new game
function playAgain(){
location.reload();
}
//Set Up
function createGamePieces(e){
getCityCountryPairs().then( data => {
let randomNums = getSetOfRandomizedValues(cityCountryPairs, data.length);
let gamePieces = setInnerHTML(data, randomNums);
capitalTiles.innerHTML = gamePieces.cTiles;
countryCards.innerHTML = gamePieces.cCards;
});
}
//Get City Country Pairs
async function getCityCountryPairs() {
let response = await fetch('node_modules/country-json/src/country-by-capital-city.json')
let data = await response.json();
return data;
}
//Get set of randomized numbers with no duplicates
function getSetOfRandomizedValues(setLength, rangeLength) {
let randomSet = new Set();
while (randomSet.size < setLength) {
randomSet.add(Math.floor(Math.random()*rangeLength));
}
return randomSet;
}
//Create InnerHTML of capitalTiles and countryCards
function setInnerHTML(array, randomNumSet) {
let cTiles = '';
let cCards = '';
let randomNumSetArray = Array.from(randomNumSet);
//Define Order Answers should be in with following array and add to innerHTML at the same time
randomNumSetArray.forEach( function(randomNum, i){
if (array[randomNum].city === null){
cTiles +=
`<span class="badge badge-dark capital" id="capital${i}" draggable="true">No Capital</span>`;
answers[i] = 'No Capital';
} else {
cTiles +=
`<span class="badge badge-dark capital" id="capital${i}" draggable="true">${array[randomNum].city}</span>`;
answers[i]=array[randomNum].city;
}
cCards += `<div class="col-sm-4">
<div class ="card my-1">
<div class = "card-body">
<h5 class="card-title text-info">
${array[randomNum].country}
</h5>
<div class="card-text border border-secondary text-secondary text-center py-3 country country${i}">
</div>
</div>
</div>
</div>`;
});
return {cTiles, cCards};
}