Skip to content

Commit 6327a69

Browse files
Merge pull request #284 from HackYourFuture-CPH/team33-frontend-advancedjs
Frontend :: Advanced JS, weeks 3 and 4
2 parents 485c9f8 + 0f3a7e1 commit 6327a69

File tree

24 files changed

+1430
-320
lines changed

24 files changed

+1430
-320
lines changed

SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,12 @@
9999
- [Week 3](courses/frontend/advanced-javascript/week3/README.md)
100100
- [Preparation](courses/frontend/advanced-javascript/week3/preparation.md)
101101
- [Session Plan](courses/frontend/advanced-javascript/week3/session-plan.md)
102+
- [Exercises](courses/frontend/advanced-javascript/week3/session-materials/exercises.md)
102103
- [Assignment](courses/frontend/advanced-javascript/week3/assignment.md)
103104
- [Week 4](courses/frontend/advanced-javascript/week4/README.md)
104105
- [Preparation](courses/frontend/advanced-javascript/week4/preparation.md)
105106
- [Session Plan](courses/frontend/advanced-javascript/week4/session-plan.md)
107+
- [Exercises](courses/frontend/advanced-javascript/week4/session-materials/exercises.md)
106108
- [Assignment](courses/frontend/advanced-javascript/week4/assignment.md)
107109
- [React](courses/frontend/react/README.md)
108110
- [Week 1](courses/frontend/react/week1/README.md)

courses/frontend/advanced-javascript/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ In this module, you will advance your JavaScript expertise to build interactive
1111
| 1. | [Array functions & Arrow functions](./week1/README.md) | [Preparation](./week1/preparation.md) | [Session Plan](./week1/session-plan.md) (for mentors) | [Assignment](./week1/assignment.md) |
1212
| 2. | [Callback functions & Asynchronous code](./week2/README.md) | [Preparation](./week2/preparation.md) | [Session Plan](./week2/session-plan.md) (for mentors) | [Assignment](./week2/assignment.md) |
1313
| 3. | [Promises & `async`/`await`](./week3/README.md) | [Preparation](./week3/preparation.md) | [Session Plan](./week3/session-plan.md) (for mentors) | [Assignment](./week3/assignment.md) |
14-
| 4. | [Classes & Advanced Promises](./week4/README.md) | [Preparation](./week4/preparation.md) | [Session Plan](./week4/session-plan.md) (for mentors) | [Assignment](./week4/assignment.md) |
14+
| 4. | [Classes & Object-Oriented Programming](./week4/README.md) | [Preparation](./week4/preparation.md) | [Session Plan](./week4/session-plan.md) (for mentors) | [Assignment](./week4/assignment.md) |
1515

1616
## Module Learning Goals
1717

courses/frontend/advanced-javascript/week2/assignment.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@ The warmup is a **little abstract**, it will get more concrete later on!
1111
1. Display the text `Called after 2.5 seconds` on the page 2.5 seconds after the script is loaded.
1212

1313
2. Create a function that takes 2 parameters: `delay` and `stringToLog`. Calling this function should display the `stringToLog` on the page after `delay` seconds. Call the function you have created with some different arguments.
14-
![second task](session-materials/carbon.png)
14+
![second task](./session-materials/carbon.png)
1515

1616
3. Create a button in html. When clicking this button, use the function you created in the previous task to display the text `Called after 5 seconds` on the page 5 seconds after the button is clicked.
1717

18-
![second task](session-materials/button-delay.gif)
18+
![second task](./session-materials/button-delay.gif)
1919

2020
4. Create two functions and assign them to two different variables. One function displays `Earth` on the page, the other displays `Saturn`. Now create a new third function that has one parameter: `planetLogFunction`. The only thing the third function should do is call the provided parameter function. Try calling the third function once with the `Earth` function and once with the `Saturn` function.
2121

22-
![second task](session-materials/planet-log.png)
22+
![second task](./session-materials/planet-log.png)
2323

2424
5. Create a button with the text "Log location". When this button is clicked, display the user's location (latitude, longitude) on the page using this [browser API](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API).
2525

26-
![second task](session-materials/log-location.gif)
26+
![second task](./session-materials/log-location.gif)
2727

2828
6. _Optional_ Now show that location on a map using e.g. the [Google maps api](https://developers.google.com/maps/documentation/javascript/tutorial)
2929

3030
7. Create a function called `runAfterDelay`. It has two parameters: `delay` and `callback`. When called the function should wait `delay` seconds and then call the provided callback function. Add an input in the HTML for the delay (in seconds) and a button; when the button is clicked, read the delay from the input and call `runAfterDelay` with that delay and a callback that displays something on the page.
3131

32-
![second task](session-materials/run-after-delay.png)
32+
![second task](./session-materials/run-after-delay.png)
3333

3434
8. Check if the user has double-clicked on the page. A double click is two clicks within 0.5 seconds. If a double click is detected, display the text "double click!" on the page.
3535

@@ -57,7 +57,7 @@ A user specifies how long time the game should be, and presses **"start game!"**
5757

5858
Here is a gif of how the site should work:
5959

60-
![session material](session-materials/fastest-clicker.gif)
60+
![Fastest presser game demo](./session-materials/fastest-clicker.gif)
6161

6262
You can implement it exactly like you want to, but here is my recommended order:
6363

courses/frontend/advanced-javascript/week3/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ In this session, you'll learn how to write asynchronous code that is both effici
66

77
- [Preparation](./preparation.md)
88
- [Session Plan](./session-plan.md) (for mentors)
9+
- [Exercises](./session-materials/exercises.md)
910
- [Assignment](./assignment.md)
1011

1112
## Session Learning Goals
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Assignment
22

3-
The assignment for this week is to build a currency calculator using [this API](https://open.er-api.com/v6/latest/USD)
3+
The assignment for this week is to build a currency calculator using [this API](https://open.er-api.com/v6/latest/USD).
4+
5+
Deliverable: a small browser application, so the user can interact with it and see the converted amount on the page.
46

57
## Technical specifications
68

7-
1. Make a request to the API and store the Exchange rates as well as a list of currencies for the dropdowns.
8-
2. User can enter an amount
9-
3. User can choose a currency to convert from(default should be EUR)
10-
4. User can choose a currency to convert to(Default should be DKK)
11-
5. Whenever amount, currency from or currency to changes we show what the amount translates to in the to currency
9+
1. Make a request to the API and use the response to obtain exchange rates and to populate the currency dropdowns.
10+
2. The user can enter an amount.
11+
3. The user can choose a currency to convert from (default: EUR).
12+
4. The user can choose a currency to convert to (default: DKK).
13+
5. When the amount, the "from" currency, or the "to" currency changes, show the equivalent amount in the "to" currency.
Binary file not shown.
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Promise chaining – what is logged?
2+
3+
Use these in class: show the code, ask “What will appear in the console, and in what order?”, then run it and compare.
4+
5+
---
6+
7+
## Task 1 — basic: sync vs `.then`
8+
9+
```js
10+
console.log("A");
11+
12+
Promise.resolve().then(() => {
13+
console.log("B");
14+
});
15+
16+
console.log("C");
17+
```
18+
19+
<details>
20+
<summary>Answer</summary>
21+
22+
Order: A, C, B
23+
24+
Synchronous code runs first (A, then C). Callbacks passed to `.then` are scheduled as microtasks and run after the current script finishes, so B appears last.
25+
26+
</details>
27+
28+
---
29+
30+
## Task 2 — values through the chain
31+
32+
```js
33+
Promise.resolve(1)
34+
.then((x) => {
35+
console.log(x);
36+
return x + 1;
37+
})
38+
.then((y) => {
39+
console.log(y);
40+
});
41+
```
42+
43+
<details>
44+
<summary>Answer</summary>
45+
46+
Logs: `1` then `2`
47+
48+
Each `.then` receives the value returned by the previous handler. Returning a plain value wraps it in a resolved promise for the next step.
49+
50+
</details>
51+
52+
---
53+
54+
## Task 3 — returning a Promise (flattening)
55+
56+
```js
57+
Promise.resolve("go")
58+
.then((s) => {
59+
console.log("a:", s);
60+
return Promise.resolve("step");
61+
})
62+
.then((t) => {
63+
console.log("b:", t);
64+
});
65+
```
66+
67+
<details>
68+
<summary>Answer</summary>
69+
70+
Logs: `a: go` then `b: step`
71+
72+
When a handler returns a Promise, the chain waits for it and passes its settled value to the next `.then` (the inner Promise is “flattened”).
73+
74+
</details>
75+
76+
---
77+
78+
## Task 4 — rejection, skipped handlers, `.catch`, recovery
79+
80+
```js
81+
Promise.resolve()
82+
.then(() => {
83+
console.log("1");
84+
throw new Error("oops");
85+
})
86+
.then(() => {
87+
console.log("2");
88+
})
89+
.catch(() => {
90+
console.log("3");
91+
})
92+
.then(() => {
93+
console.log("4");
94+
});
95+
```
96+
97+
<details>
98+
<summary>Answer</summary>
99+
100+
Logs: `1`, `3`, `4`
101+
102+
The error skips the next `.then` (so `2` never runs). `.catch` handles the rejection; a successful `catch` returns a fulfilled promise, so the following `.then` still runs (`4`).
103+
104+
</details>
105+
106+
---
107+
108+
## Task 5 — multiple `.catch` and `.then` in one chain
109+
110+
```js
111+
Promise.resolve()
112+
.then(() => {
113+
console.log("1");
114+
throw "first-error";
115+
})
116+
.catch((err) => {
117+
console.log("catch-A", err);
118+
return "recovered";
119+
})
120+
.then((value) => {
121+
console.log("2", value);
122+
throw "second-error";
123+
})
124+
.catch((err) => {
125+
console.log("catch-B", err);
126+
})
127+
.then(() => {
128+
console.log("3");
129+
});
130+
```
131+
132+
<details>
133+
<summary>Answer</summary>
134+
135+
Logs: `1`, `catch-A first-error`, `2 recovered`, `catch-B second-error`, `3`
136+
137+
The first `throw` is handled by `catch-A`, which returns `"recovered"`, so the chain continues fulfilled and `2` runs with that value. The next `throw` is handled by `catch-B`; a successful `catch` still yields a fulfilled promise, so the final `.then` runs (`3`). The second `.catch` never sees `first-error` because `catch-A` already handled it.
138+
139+
</details>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Mentors demo – Promises & `async`/`await`
2+
3+
In-session live coding for **Week 3** (Advanced JavaScript). The demo walks through `fetch` with **JSONPlaceholder**, `async`/`await`, consuming promises with `.then()` / `.catch()`, creating promises with `new Promise`, `try` / `catch` with async code, `Promise.all`, and an optional promise microtask loop. You implement the worksheet during class; the solution file is the finished version.
4+
5+
---
6+
7+
## Files in this folder
8+
9+
| File | Purpose |
10+
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
11+
| **index.js** | Worksheet: section banners, `// Task:` lines, and `// Next:` hints. Only `getUser` and `promiseLoop` are declared; you add the rest while teaching. Use this file when leading the session. |
12+
| **index-solution.js** | Full implementation: `showOutput`, `getUser`, promise consumption, timed and pizza promises, `try` / `catch` fetch, `Promise.all`, and `promiseLoop`. |
13+
| **index.html** | Minimal page that loads `index.js`. Add markup (e.g. `<pre id="out">`) when you want on-page output; the solution’s `showOutput` writes to `#out`. |
14+
| **style.css** | Basic layout and styles for `#out` (and `main` if you use it). |
15+
16+
---
17+
18+
## Where to find tasks and how they are marked
19+
20+
Everything lives in **index.js**. Search for `// ==========` for section breaks, `// Task:` for what to build, and `// Next:` for suggested links to the trainee exercises.
21+
22+
---
23+
24+
## How the code works
25+
26+
### URLs (JSONPlaceholder)
27+
28+
- **`USER_URL`**`https://jsonplaceholder.typicode.com/users/1`
29+
- **`POST_URL`**`https://jsonplaceholder.typicode.com/posts/1`
30+
- **`TODO_URL`**`https://jsonplaceholder.typicode.com/todos/1` (optional extra)
31+
32+
### Solution-only helpers and functions
33+
34+
- **`showOutput(text)`** – Sets `textContent` on `#out` when that element exists.
35+
- **`getUser()`**`async` `fetch` of **`USER_URL`**, then `.json()`, then `showOutput` with stringified user data.
36+
- **`loadOneResourceWithThen()`** – Same resource with `.then` / `.catch` only (no `async`/`await`).
37+
- **`oneSecondMessage()`** – Promise that resolves after one second, then shows `"It worked"`.
38+
- **`demoOrderPizza()`** – Delayed resolve or reject, then shows pizza or error text.
39+
- **`getUserWithTryCatch()`** – Same fetch pattern as `getUser` with `try` / `catch` and errors on the page.
40+
- **`demoPromiseAll()`** – Fetches **`USER_URL`** and **`POST_URL`** in parallel, then shows a short two-line summary.
41+
- **`promiseLoop()`** – Schedules endless microtasks (illustration only; can freeze the tab if called).
42+
43+
### Callback hell
44+
45+
Not implemented in these files; use the session plan (e.g. npm `q` or your own example on the board).

0 commit comments

Comments
 (0)