|
| 1 | +<!-- .slide: id="lesson13" --> |
| 2 | + |
| 3 | +# JavaScript Course - Fall 2025 |
| 4 | + |
| 5 | +Lesson 13, Tuesday, 2025-11-11 |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +### Lesson overview |
| 10 | + |
| 11 | +- Recap |
| 12 | +- JavaScript Object Notation (JSON) |
| 13 | +- `fetch` API |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +### Recap |
| 18 | + |
| 19 | +What appears on the web page after this code runs? |
| 20 | + |
| 21 | +```js |
| 22 | +let el = document.createElement("button"); |
| 23 | +el.textContent = "Click me!"; |
| 24 | +``` |
| 25 | + |
| 26 | +Answer: Nothing, because we never _append_ the element anywhere |
| 27 | + |
| 28 | +<!-- .element: class="fragment" --> |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +### Recap |
| 33 | + |
| 34 | +Corrected code: |
| 35 | + |
| 36 | +```js |
| 37 | +let el = document.createElement("button"); |
| 38 | +el.textContent = "Click me!"; |
| 39 | +document.body.appendChild(el); |
| 40 | +``` |
| 41 | + |
| 42 | +Now, the button is appended to the body. It'll appear _after_ all other elements. |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +<!-- .slide: id="JSON" --> |
| 47 | + |
| 48 | +# JSON |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +In JavaScript, we can put all keys of objects in quotes. This is optional. |
| 53 | +We can still access the keys without quotes: |
| 54 | + |
| 55 | +```js |
| 56 | +let me = { |
| 57 | + "name": "John", |
| 58 | + "lastName": "Doe", |
| 59 | + "hobbies": ["Eat", "Sleep"], |
| 60 | +}; |
| 61 | + |
| 62 | +console.log(me.name); // "John" |
| 63 | +``` |
| 64 | + |
| 65 | +--- |
| 66 | + |
| 67 | +### Transferring data over the internet |
| 68 | + |
| 69 | +Let's say we want to transfer the object `me` over the internet. We can't just send the object, because the internet only understands strings. |
| 70 | + |
| 71 | +We could send a string like "Hi my name is John Doe and I like to eat and sleep". But then how would the receiver know what is the name, what is the last name, and what are the hobbies? |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +### JSON |
| 76 | + |
| 77 | +- JSON stands for **J**ava**S**cript **O**bject **N**otation |
| 78 | +- When we exchange data between a browser and a server, we can only exchange string (not objects, arrays, numbers, booleans...) |
| 79 | +- JSON is a string representation of JavaScript objects |
| 80 | +- JSON can be easily transferred (and stored) |
| 81 | +- The notation is very close to JavaScript objects, easy to handle from JavaScript. No complex transformation required. |
| 82 | +- More info: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +- Must contain only these types: |
| 87 | + - `string`, `number`, `boolean`, `array` |
| 88 | + - `null` |
| 89 | + - another JSON `object` |
| 90 | +- These types are **forbidden**: |
| 91 | + - `function`, `undefined` |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +JavaScript: |
| 96 | + |
| 97 | +```js |
| 98 | +{ name: 'Alan', age: 42 } |
| 99 | +``` |
| 100 | + |
| 101 | +JSON: |
| 102 | + |
| 103 | +```js |
| 104 | +'{"name":"Alan","age":42}' |
| 105 | +``` |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +### Web APIs |
| 110 | + |
| 111 | +There are a lot of services on the internet that transfer data via JSON. |
| 112 | + |
| 113 | +Example: https://restcountries.com/v3.1/lang/spanish |
| 114 | +Documentation: https://restcountries.com/ |
| 115 | + |
| 116 | +Find more at https://apilist.fun, https://rapidapi.com/hub or https://apis.guru |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +OK great, but how can we access that from JavaScript? |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +<!-- .slide: id="fetch" --> |
| 125 | + |
| 126 | +# fetch API |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +- Fetching data from the internet might take a lot of time |
| 131 | +- We must not block our browser while waiting for the reply! |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +### Synchronous vs Asynchronous |
| 136 | + |
| 137 | +Imagine you order a pizza: |
| 138 | + |
| 139 | +- **Synchronous:** You stand at the counter and wait until your pizza is ready. You don't do anything else until you get your pizza. |
| 140 | +- **Asynchronous:** You order the pizza, give your phone number, and go learn JavaScript. When the pizza is ready, they call you to pick it up. |
| 141 | + |
| 142 | +**Synchronous code** waits for each task to finish before moving on. |
| 143 | +**Asynchronous code** can start a task and move on to other things, then come back when the task is done. |
| 144 | + |
| 145 | +<!-- .slide: style="font-size:80%" --> |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +### Synchronous function call |
| 150 | + |
| 151 | +```js |
| 152 | +function add(a, b) { |
| 153 | + return a + b; |
| 154 | +} |
| 155 | +let result = add(1, 2); |
| 156 | +``` |
| 157 | + |
| 158 | +`add(1, 2)` is a synchronous function call. The function is called, and the result is returned immediately. No other code runs in between. While `add` runs, the entire web page is blocked. |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +### Asynchronous function call |
| 163 | + |
| 164 | +```js |
| 165 | +async function add(a, b) { |
| 166 | + return a + b; |
| 167 | +} |
| 168 | +let result = add(1, 2); |
| 169 | +``` |
| 170 | + |
| 171 | +We define `add` with the additional `async` keyword. This tells JavaScript that the function is asynchronous. The function is called, but the result is not returned immediately. The function returns a promise. |
| 172 | + |
| 173 | +--- |
| 174 | + |
| 175 | +So great, we can define a function asynchronously. |
| 176 | + |
| 177 | +But how do we get the result? |
| 178 | + |
| 179 | +<!-- .element: class="fragment" --> |
| 180 | + |
| 181 | +--- |
| 182 | + |
| 183 | +### await |
| 184 | + |
| 185 | +```js |
| 186 | +async function add(a, b) { |
| 187 | + return a + b; |
| 188 | +} |
| 189 | +async function main() { |
| 190 | + let result = await add(1, 2); |
| 191 | +} |
| 192 | +main(); |
| 193 | +``` |
| 194 | + |
| 195 | +We can use the `await` keyword to wait for the result of an asynchronous function. The `await` keyword can only be used inside an `async` function. |
| 196 | + |
| 197 | +--- |
| 198 | + |
| 199 | +### fetch |
| 200 | + |
| 201 | +`fetch` is an `async` function that we can use to download content from the internet asynchronously: |
| 202 | + |
| 203 | +```js |
| 204 | +async function main() { |
| 205 | + let response = await fetch("https://restcountries.com/v3.1/lang/spanish"); |
| 206 | + let data = await response.json(); |
| 207 | + console.log(data); |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +--- |
| 212 | + |
| 213 | +### Tasks |
| 214 | + |
| 215 | +1. Download the data from https://restcountries.com/v3.1/name/deutschland (or any country you like) in JavaScript and log the response to console |
| 216 | +1. Create a web page that displays the country's official name, its area and its population |
| 217 | +1. Bonus: Show the capital cities |
| 218 | +1. Bonus: Display the flag and the coat of arms of the country as SVG file. (Hint: use `img` element and set the `src` attribute). |
| 219 | + |
| 220 | +<!-- .slide: style="font-size:80%" --> |
| 221 | + |
| 222 | +--- |
| 223 | + |
| 224 | +### More Bonus |
| 225 | + |
| 226 | +1. Add an input field and a button to your page. When the user clicks the button, show the details about the country that the user entered in the input field. |
| 227 | +1. Sometimes, more than one country can be returned. Try entering "united" in your input field. Extend your web page to show _all_ countries that match the user's input. |
| 228 | +1. Show all the bordering countries (field: `borders`) as buttons. Example: Germany has a neighbour called `AUT`. Create a button with text content `AUT` and when the user clicks the button, the page should display information about this country (hint: use the `alpha` API, e.g: https://restcountries.com/v3.1/alpha/aut) |
| 229 | + |
| 230 | +<!-- .slide: style="font-size:80%" --> |
| 231 | + |
| 232 | +--- |
| 233 | + |
| 234 | +### Dogs |
| 235 | + |
| 236 | +https://dog.ceo/dog-api/documentation/random |
| 237 | + |
| 238 | +> Display a random dog image |
| 239 | +
|
| 240 | +https://kinduff.github.io/dog-api/ |
| 241 | + |
| 242 | +> Fetch any random dog fact |
| 243 | +
|
| 244 | + |
| 245 | +--- |
| 246 | + |
| 247 | +### Task |
| 248 | + |
| 249 | +Create a simple webpage, that fetches a random dog image from the API. Pair it with the second API and show a random dog fact together with image. |
| 250 | + |
| 251 | +--- |
| 252 | + |
| 253 | +## Holidays |
| 254 | + |
| 255 | +https://date.nager.at/Api |
| 256 | + |
| 257 | + |
| 258 | +> Query the holidays of over 100 countries |
| 259 | +
|
| 260 | +--- |
| 261 | + |
| 262 | +### Task |
| 263 | + |
| 264 | +Show the holidays in Germany for 2026. |
| 265 | + |
| 266 | +BONUS: Add a dropdown to select holidays for one state only. |
| 267 | + |
| 268 | + |
0 commit comments