-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathscript.js
More file actions
172 lines (152 loc) · 7.4 KB
/
Copy pathscript.js
File metadata and controls
172 lines (152 loc) · 7.4 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
/* ============================================================
COUNTRY EXPLORER — script.js
------------------------------------------------------------
THE PATTERN (this is the whole lab):
1. Get user input
2. Fetch data from an API
3. Convert the response to JSON
4. Pull the piece of data you need off the response
5. Drop it into the page with .textContent / .src
Steps 1-6 below are built together as an instructor demo.
Steps 7-10 are your team's mission — they reuse the exact
same pattern shown in Step 6, just with different data.
============================================================ */
// --- Element references (grab everything once, up front) -----
const searchBtn = document.getElementById("searchBtn");
const countryInput = document.getElementById("countryInput");
const loadingEl = document.getElementById("loading");
const errorEl = document.getElementById("errorMessage");
const resultCard = document.getElementById("resultCard");
const flagImg = document.getElementById("flagImg");
const countryNameEl = document.getElementById("countryName");
const capitalEl = document.getElementById("capitalValue");
const regionEl = document.getElementById("regionValue");
const populationEl = document.getElementById("populationValue");
const languagesEl = document.getElementById("languagesValue");
/* ------------------------------------------------------------
STEP 1: Connect the button's click event.
------------------------------------------------------------ */
searchBtn.addEventListener("click", fetchCountry);
/* ------------------------------------------------------------
STEP 2: Create fetchCountry()
------------------------------------------------------------ */
async function fetchCountry() {
const countryName = countryInput.value.trim();
if (!countryName) return; // nothing typed, nothing to do
/* ------------------------------------------------------------
STEP 3: Show "Loading..."
------------------------------------------------------------ */
showLoading();
try {
/* ------------------------------------------------------------
STEP 4: Build the fetch request
------------------------------------------------------------
The old v3.1 API is retired. v5 requires an API key sent as
an Authorization header, and (for browser requests) your
API key must allow-list your origin (127.0.0.1 / localhost/ or your live preview url without the HTTPS)
on the API Keys page at restcountries.com.
------------------------------------------------------------ */
const API_KEY = "";
const url =
`https://api.restcountries.com/countries/v5?q=${encodeURIComponent(countryName)}` +
`&response_fields=names.common,capitals,region,population,languages,flag.url_png,flag.description`;
const response = await fetch(url, {
headers: { Authorization: `Bearer ${API_KEY}` },
});
if (!response.ok) {
throw new Error("Country not found");
}
/* ------------------------------------------------------------
STEP 5: Convert response -> JSON
------------------------------------------------------------
v5 wraps results in { data: { objects: [...] } }
------------------------------------------------------------ */
const data = await response.json();
const objects = data.data.objects;
if (!objects || objects.length === 0) {
throw new Error("Country not found");
}
const country = objects[0];
/* ------------------------------------------------------------
STEP 6: Display ONE property. Start simple.
Display only: Country Name.
------------------------------------------------------------ */
countryNameEl.textContent = country.names.common;
/* ============================================================
TEAM BUILD STARTS HERE
============================================================ */
/* ============================================================
TEAM BUILD STARTS HERE
The instructor demo stops at Step 6. Steps 7-10 are your
team's mission. Use the exact pattern from Step 6 above.
============================================================ */
/* ------------------------------------------------------------
STEP 7: Display the Flag
------------------------------------------------------------
TODO:
- The image URL lives at: country.flags.png
- Set flagImg.src to that URL
- Set flagImg.alt to something descriptive, e.g.
`Flag of ${country.name.common}`
flagImg.src = ???
flagImg.alt = ???
------------------------------------------------------------ */
/* ------------------------------------------------------------
STEP 8: Display Capital, Region, Population
------------------------------------------------------------
TODO:
- Capital: country.capital[0] <-- careful, it's an ARRAY
- Region: country.region
- Population: country.population <-- a raw number
Hint: number.toLocaleString() formats 129000000 as
"129,000,000" — much easier to read.
capitalEl.textContent = ???
regionEl.textContent = ???
populationEl.textContent = ???
------------------------------------------------------------ */
/* ------------------------------------------------------------
STEP 9: Display Languages
------------------------------------------------------------
TODO:
- country.languages is an OBJECT, not an array, e.g.:
{ fra: "French", eng: "English" }
- Object.values(country.languages) turns that into an
array of names: ["French", "English"]
- .join(", ") turns that array into one readable string:
"French, English"
languagesEl.textContent = ???
------------------------------------------------------------ */
/* ------------------------------------------------------------
STEP 10a: Improve the experience — clean up on SUCCESS
------------------------------------------------------------
TODO:
- Hide the loading state: hideLoading();
- Reveal the result card: resultCard.classList.remove('hidden');
- Make sure old errors are gone: errorEl.classList.add('hidden');
------------------------------------------------------------ */
} catch (error) {
/* ------------------------------------------------------------
STEP 10b: Improve the experience — error handling
------------------------------------------------------------
TODO:
- Hide the loading state
- Hide the result card (a previous search may have shown one)
- Show a friendly message in errorEl, e.g.:
errorEl.textContent =
`We couldn't find "${countryName}". Check the spelling and try again.`;
errorEl.classList.remove('hidden');
------------------------------------------------------------ */
console.error(error);
}
}
/* ------------------------------------------------------------
Helper functions — already built for you.
------------------------------------------------------------ */
function showLoading() {
loadingEl.classList.remove('hidden');
errorEl.classList.add('hidden');
resultCard.classList.add('hidden');
}
function hideLoading() {
loadingEl.classList.add('hidden');
}