-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
165 lines (136 loc) · 4.53 KB
/
script.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
let countries;
let totalPopulation;
let continentsArray = []
// #region || search input + mic
let previousSearch = "";
$(".search-btn").click(searchFilterClick)
function searchFilterClick() {
if (previousSearch == $(".search-text-input").val()) {
return
}
previousSearch = $(".search-text-input").val()
if ($(".search-text-input").val() == "") {
fetchAll()
return
}
fetchFew()
}
// #region || speech recognition
try {
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition
const recognition = new SpeechRecognition()
recognition.interimResults = true
recognition.lang = "en-US"
$(".search-mic-btn").click(e => {
console.log("listening...")
recognition.start()
$(".search-mic-btn").addClass("listening")
})
recognition.onresult = function(e) {
$(".search-text-input").val(e.results[0][0].transcript)
if (e.results[0].isFinal == true) {
console.log("completed listening process")
searchFilterClick()
}
}
recognition.onspeechend = function() {
recognition.stop()
console.log("stopped listening: speech ended.")
$(".search-mic-btn").blur()
$(".search-mic-btn").removeClass("listening")
}
recognition.onerror = function(event) {
console.log('Stopped listening due to error: ' + event.error)
recognition.stop()
$(".search-mic-btn").blur()
$(".search-mic-btn").removeClass("listening")
}
} catch (error) {
console.warn("Microphone was removed due to lack of browser support. ERR:", error)
$(".search-mic-btn").remove()
}
// #endregion
// #endregion
$("#all-btn").click(fetchAll)
function fetchAll() {
const url = `https://restcountries.com/v3.1/all`
$.get(url, (data) => {
countries = data
console.log(countries)
renderCountries()
})
}
function renderCountries() {
clearCountries()
if (countries.length < 1) {
clearContinents()
$(".countries-container").append(`<h2>No countries found 😢</h2>`)
$("#total-countries-num").text(0)
$("#total-population-num").text(0)
$("#avg-population-num").text(0)
return
}
totalPopulation = 0;
continentsArray = []
for (let each of countries) {
$(".countries-container").append(`
<div class="card-container">
<h2 id="country-name">${each.name.common}</h2>
<h2>Population: <span id="population-num">${each.population}</span></h2>
</div>
`)
totalPopulation += each.population;
continentsArray = continentsArray.concat(each.continents)
}
updateSummary()
renderContinents()
}
function clearCountries() {
$(".countries-container").html("")
}
function clearContinents() {
$(".continents-container").html("")
}
function updateSummary() {
$("#total-countries-num").text(countries.length)
$("#total-population-num").text(totalPopulation)
let avgPopulation = totalPopulation / countries.length;
$("#avg-population-num").text(Math.floor(avgPopulation))
}
function renderContinents() {
console.log(continentsArray)
clearContinents()
// from stackoverflow:
let reducedContinents = continentsArray.reduce((grouping, item) => {
// If the current list item does not yet exist in grouping, set it's initial count to 1
if(grouping[item] === undefined) {
grouping[item] = 1;
}
// If current list item does exist in grouping, increment the count
else {
grouping[item] ++;
}
return grouping;
}, {})
console.log(reducedContinents)
for(let each in reducedContinents) {
$(".continents-container").append(`
<div class="card-container">
<h2 id="continent-name">${each}</h2>
<h2>Countries: <span id="country-count">${reducedContinents[each]}</span></h2>
</div>
`)
}
}
function fetchFew() {
const url = `https://restcountries.com/v3.1/name/${$(".search-text-input").val()}`
$.get(url, (data) => {
countries = data
console.log(countries)
renderCountries()
}).fail(() => {
countries = []
renderCountries()
})
}
fetchAll()