You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Week1/MAKEME.md
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -9,15 +9,15 @@
9
9
10
10
## **1. Practice the concepts**
11
11
12
-
Before we head into the exercises, it might be nice to do some interactive exercises first! In the following resource you'll find some exercises that'll teach you the basics of the interaction between JavaScript and the DOM
12
+
Before we head into the exercises, it might be nice to do some interactive exercises first! In the following resource you'll find some exercises that'll teach you the basics of the interaction between JavaScript and the DOM:
> Inside of your `JavaScript2` fork, create a folder called `week1`. Inside of that folder, create a folder called `js-exercises`. For all the following exercises create a new `.js` file in that folder (5 files in total). Make sure the name of each file reflects its content: for example, the filename for exercise one could be `bookList.js`.
19
19
20
-
### 1. The book list
20
+
**Exercise 1: The book list**
21
21
22
22
I'd like to display my three favorite books inside a nice webpage!
23
23
@@ -42,7 +42,7 @@ const books = [
42
42
4. Add an <img> to each book that links to a URL of the book cover.
43
43
5. Change the style of the book depending on whether you have read it (green) or not (red).
44
44
45
-
### 2. About me
45
+
**Exercise 2: About me**
46
46
47
47
Start with this HTML and save it as "about_me.html":
48
48
@@ -72,7 +72,7 @@ Start with this HTML and save it as "about_me.html":
72
72
5. (In the HTML head) Add a style tag that sets a rule for .list-item to make the color red.
73
73
6. Create a new img element and set its src attribute to a picture of you. Append that element to the page.
74
74
75
-
### 3. The logo hijack
75
+
**Exercise 3: The logo hijack**
76
76
77
77
No homepage is safe from the logo bandit! Everytime he sees a Google Logo he replaces it with a logo from HackYourfuture instead: https://www.hackyourfuture.dk/static/logo-dark.svg.
78
78
@@ -82,7 +82,7 @@ In this exercise you're expected to write a JavaScript function that can be exec
82
82
2. Find out how to select the element that contains the Google logo, and store it in a variable
83
83
3. Modify the source and sourceset of the logo so that it's replaced by the HackYourFuture logo instead
84
84
85
-
### 4. What's the time?
85
+
**Exercise 4: What's the time?**
86
86
87
87
Why wear a watch when you can check the time, live in your webpage?
88
88
@@ -91,7 +91,7 @@ Why wear a watch when you can check the time, live in your webpage?
91
91
3. Inside the JS file, write a function that adds the current time to the webpage. Make sure it's written in the HH:MM:SS notation (hour, minute, second). Hint: use `setInterval()` to make sure the time stays current
92
92
4. Have the function execute when it's loading in the browser
93
93
94
-
### 5. The cat walk
94
+
**Exercise 5: The cat walk**
95
95
96
96
Start with this webpage, which has a single img tag of an animated GIF of a cat walking.
Copy file name to clipboardExpand all lines: Week1/README.md
+3-6Lines changed: 3 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -92,10 +92,7 @@ The actual process of transforming HTML, CSS and JavaScript into a user-viewable
92
92
93
93
## 3. What is DOM Manipulation?
94
94
95
-
**DOM manipulation** refers to the activity of selecting and modifying DOM elements. The main reason why this is done is that **you want to show the user different things depending their activity**, for example:
96
-
97
-
- You click on a [hamburger menu icon](https://bit.ly/2Yr4O7Z) and a navigation menu slides in
98
-
- You scroll down and the elements of
95
+
**DOM manipulation** refers to the activity of selecting and modifying DOM elements. The main reason why this is done is that **you want to show the user different things depending their activity**, for example if you click on a [hamburger menu icon](https://bit.ly/2Yr4O7Z) and a navigation menu slides in.
99
96
100
97
Finding the right elements is called **traversing the DOM**. Traversing the DOM essential means: using JavaScript to select certain elements within the DOM in order to modify them (change color, size or make them disappear, for example).
101
98
@@ -156,8 +153,8 @@ Test this code out by copying and pasting it in the Developer Console of your br
156
153
This is DOM manipulation in its simplest form. It goes in three essential steps:
157
154
158
155
(1) An event happens ("User clicks on the page")
159
-
(2) JavaScript is aware and looks for this specific user action (The browser is listening for the event*), in this case a `click` event)
160
-
(3) JavaScript modifies the DOM as a result (\_A function that makes the body background color black is executed*)
156
+
(2) JavaScript is aware and looks for this specific user action (The browser is listening for the event, in this case a `click` event)
157
+
(3) JavaScript modifies the DOM as a result (A function that makes the body background color black is executed)
161
158
162
159
The second step is called **listening for the event**. We do this by using a by the browser predefined function called `addEventListener()`, which we get from the `document` object in the browser. The browser needs to listen to the event in order to know what it should do ("make the body background color black") in case a certain event (`click`) happens to a certain element (`body`).
@@ -90,8 +88,6 @@ Study the following resources to learn more about the importance of callbacks:
90
88
91
89
## 3. Array Functions
92
90
93
-
In programming we're always trying to make things easier on ourselves.
94
-
95
91
There are different ways of dealing with arrays. The most common way is by using a loop and then writing custom logic inside it in order to manipulate the values. This solution works, but it comes at several disadvantages.
96
92
97
93
1. The first disadvantage is that using loops requires us to write custom logic for each use case. This can lead to repeated code, which we always want to [avoid](https://www.youtube.com/watch?v=IGH4-ZhfVDk)
@@ -151,7 +147,7 @@ In simple terms, the `Event Loop` is a mechanism that operates in the browser. I
151
147
1. Heap. This is where the browser assigns space in memory to each process
152
148
2. Call Stack. This is the amount of JavaScript commands (read: function calls and events) that need to be executed
153
149
3. Web APIs. These are objects (like the document) and functions (like XMLHttpRequest) that can be used within the JavaScript commands found in the Call Stack
154
-
4. Callback Queue. This is the "waiting line" [ajksdbjkasbdjkasbnjkdbajksbdkj]
150
+
4. Callback Queue. This is the "waiting line" for asynchronous function calls
155
151
156
152
To see it in action check out the following resources:
In this exercise you'll be presented with 2 code snippets. Your task is to guess the output and write out your reasoning, for each single one, in 50 words or less.
59
+
In this exercise you'll be presented with a code snippet. Your task is to guess the output and write out your reasoning in 50 words or less.
60
60
61
61
```js
62
+
// Snippet
62
63
let a =10;
63
64
constx= (function() {
64
65
a =12;
@@ -70,7 +71,12 @@ const x = (function() {
70
71
x();
71
72
```
72
73
74
+
**Exercise 4: Guess more**
75
+
76
+
In this exercise you'll be presented with another code snippet. Guess the output and write out your reasoning in 50 words or less.
77
+
73
78
```js
79
+
// Snippet
74
80
constx=9;
75
81
functionf1(val) {
76
82
val = val +1;
@@ -88,25 +94,6 @@ f2(y);
88
94
console.log(y);
89
95
```
90
96
91
-
**Exercise 4: Make the sandwich**
92
-
93
-
Mrs Potts the school dinner lady is tired of all the global sandwich variables getting under her feet and tripping her up all the time. She needs help cleaning her kitchen. Will you help her?
94
-
95
-
She would like it very much if you would build a sandwich machine for her, but wrap it in a closure so as to keep everything neat.
96
-
97
-
1. Create a self executing function
98
-
2. Within the closure, create three little functions to add the bread, spread the butter and add the jam. These little methods should use console.log to write a string representing their action to the DOM, e.g. "Now spreading the jam!"
99
-
3. Assign makeSandwich to the global window object, thus smuggling it out of the closure.
100
-
4. Call makeSandwich from outside the closure
101
-
102
-
It should log to the console the following message:
103
-
104
-
```markdown
105
-
Adding bread
106
-
Now spreading the butter
107
-
Spreading jam
108
-
```
109
-
110
97
**Exercise 5: The lottery machine**
111
98
112
99
Don't you just love the thrill of the lottery? What if I told you we can make our own lottery machine? Let's get started!
@@ -126,6 +113,8 @@ The function should call the second callback if the array value is divisible by
126
113
127
114
Both functions should be called if the array value is divisible by both 3 and 5.
Copy file name to clipboardExpand all lines: Week3/README.md
+76-26Lines changed: 76 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -8,12 +8,10 @@ These are the topics for week 3:
8
8
- Global vs. local (function and block)
9
9
- Const and let
10
10
2. Hoisting
11
-
- What is it
12
-
- When does it happen
13
-
- Why do we need to know it?
11
+
- When does hoisting happen?
14
12
3. Closures
15
13
- Execution context
16
-
-Defining a function within a function
14
+
-Why do we need closures?
17
15
4. Thinking like a programmer II
18
16
19
17
## 1. Scope
@@ -36,34 +34,69 @@ It depends on `context` for me to know what really happened.
36
34
37
35
Let's draw the line to programming. Simply put, just like context gives meaning to a word, `scope` gives meaning to a variable/object.
38
36
39
-
The meaning is defined by whether or not the variable is accessible or not. If the variable is not within the scope
37
+
That meaning is defined by whether or not the variable is accessible. If the variable is not within the "scope" (you could also call it "reach") of any given code block, it can't access it.
In any given application, there is usually one global scope. But there can be many local scopes
45
+
Scope can be divided into two basic types: global and local scope. In any given application, there is one global scope. But there can be many local scopes. Any variable declared outside of a function belongs to the global scope and is therefore accessible from anywhere in the code. Variables declared within a local scope are only accessible within that scope.
48
46
49
-
-[Understanding Scope in JavaScript](https://www.youtube.com/watch?v=SBjf9-WpLac)
50
-
-[Everything you wanted to know about JavaScript scope](https://ultimatecourses.com/blog/everything-you-wanted-to-know-about-javascript-scope)
47
+
Local scope can be further divided into two categories: function and block. Let's look at **function scope** first.
51
48
52
-
### Const and let
49
+
A unique local scope gets created whenever a function is declared. The variables declared within will only be accessible within that scope, nowhere else. This makes it possible to declare variables within the same name in each different local scope. This also means that it's not possible to refer to a variable declared in one local scope, within another local scope.
53
50
54
-
> Quick refresher: Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.
51
+
```js
52
+
functioncreateLocalScope() {
53
+
constlocalVariable='this variable can only be accessed within this function';
54
+
console.log(localVariable);
55
+
56
+
constlocalOnlyHere='This variable can only be accessed here, nowhere else';
57
+
}
58
+
59
+
functioncreateAnotherLocalScope() {
60
+
constlocalVariable=
61
+
"Even though this variable has the same name, it has nothing to do with the other localVariable, because it doesn't exist outside of that function";
62
+
console.log(localVariable);
63
+
console.log(localOnlyHere);
64
+
}
65
+
66
+
createLocalScope();
67
+
createAnotherLocalScope();
68
+
```
55
69
56
-
In JavaScript we used to define variables using the keyword `var`:
70
+
However, variables declared within the global scope can be accessed anywhere! Actually, that's the very purpose of global scope. In the context of functions this means that you don't have to pass it as an argument, but that you can directly refer to it within the function.
57
71
58
72
```js
59
-
var myName ='Mohammed';
73
+
constglobalVariable='This variable can be accessed wherever in the code';
74
+
75
+
functionaccessGlobalVariable() {
76
+
console.log(globalVariable);
77
+
}
78
+
console.log(globalVariable);
79
+
accessGlobalVariable();
60
80
```
61
81
62
-
However
82
+
The second type of local scope is called **block scope**. A block, generally speaking, is any code wrapped within `{ }`. This includes conditional statements (`if` and `switch`) and loops (`for`, `while` and `do/while`).
83
+
84
+
Go through the following resources to learn more about `scope`:
85
+
86
+
-[JavaScript: Introduction to Scope (function scope, block scope)](https://dev.to/sandy8111112004/javascript-introduction-to-scope-function-scope-block-scope-d11)
87
+
-[Understanding Scope in JavaScript](https://www.youtube.com/watch?v=SBjf9-WpLac)
88
+
-[Everything you wanted to know about JavaScript scope](https://ultimatecourses.com/blog/everything-you-wanted-to-know-about-javascript-scope)
89
+
90
+
### Const and let
63
91
64
-
In programming, we generally want to keep things as simple as they can be. We even have a name for that: [KISS](https://thevaluable.dev/kiss-principle-explained/) (Keep it Simple, Stupid!)
92
+
As mentioned in the previous module, we prefer to declare variables using `const` and `let`. This is because the keywords are more descriptive and restrictive. This makes them easier to work with.
93
+
94
+
In relation to scope both also behave differently: they are block scoped. This means that they can be accessed from outside a `{ }`.
95
+
96
+
Go through the following resources to learn more about this:
65
97
66
98
-[How let and const are scoped in JavaScript](https://wesbos.com/javascript-scoping/)
99
+
-[Should you truly never use var?](https://dev.to/johnwolfe820/should-you-never-truly-use-var-bdi)
67
100
68
101
## 2. Hoisting
69
102
@@ -73,45 +106,62 @@ If you look up the term "hoisting" in any dictionary, you'll find something like
73
106
74
107
A simple example of hoisting is the hoisting of a flag on a pole. You pull on the rope and slowly but surely the flag gets raised up.
75
108
76
-
### What is it
109
+
In JavaScript, hoisting refers to the mechanism of the browser's JavaScript compiler to bring every function and variable declaration to the top of their `scope`, before it starts executing anything. This can be either global or local scope, depending on where it is defined.
77
110
78
-
In JavaScript, hoisting refers to
111
+
However, this does NOT mean that the actual value given to the variable or function will also be hoisted. It's just the declaration: that there are variables/functions that exist with that name.
79
112
80
-
### When does it happen
113
+
### When does hoisting happen?
81
114
82
-
### Why do we need to know it?
115
+
Hoisting happens during `compile-time`.
83
116
84
117
When you execute your JavaScript code, the interpreter goes through the code twice. The first time is called the `compile-time`, which is when your code is made ready to be executed: there will be safety checks, small optimizations and making sure the syntax is written correctly.
85
118
86
119
The second time is called `run-time`, which is where it actually executes your code by going through it line by line, doing the assignments, calling the functions, etc.
87
120
88
-
Hoisting happens during `compile-time`.
121
+
For more research, check out the following:
122
+
123
+
-[What is Hoisting in JavaScript?](https://medium.com/javascript-in-plain-english/https-medium-com-javascript-in-plain-english-what-is-hoisting-in-javascript-a63c1b2267a1h)
89
124
90
125
## 3. Closures
91
126
92
-
Simply put, a closure is a function that is defined inside another function.
127
+
Simply put, a closure is a function that is defined inside another function. This special function has access to the outer scope (and thus its variables), the scope that's created by the function that contains the closure function.
128
+
129
+
That's nice and all, but in order to really understand what it is and why we need it we need to take a look at another concept.
93
130
94
131
### Execution context
95
132
96
-
In your programming journey you'll learn that many concepts overlap one another.
133
+
The execution context roughly equates to the 'environment' a function executes in. This consists of the following:
134
+
135
+
- The variable scopes
136
+
- Function arguments
137
+
- The value of the `this` object (more on that in JavaScript3)
138
+
139
+
Checkout the following to learn more about why this is important:
140
+
141
+
-[What is the Execution Context & Stack in JavaScript?](http://davidshariff.com/blog/what-is-the-execution-context-in-javascript/)
142
+
143
+
### Why do we need closures?
144
+
145
+
Closures are commonly used to give objects data privacy. We don't want certain data to be available globally. Think of it as "keeping something a secret. Take, for example, the following situation:
97
146
98
-
An execution context is
147
+
> You want to log in to your email account, so you need a password. Usually you have that password in your head, or somewhere written down in a place that can only be accessed in a certain way. It's not out there in public, able to be accessed by anyone.
99
148
100
-
### Defining a function within a function
149
+
In this example your password is the data you want to keep locally scoped. Your act of logging in is the inner function. The outer function could be your act of being on the computer, where your password is stored in a file somewhere.
101
150
102
151
For further study please check the following resources:
103
152
104
153
-[The Ultimate Guide to Execution Contexts, Hoisting, Scoping and Closures in JavaScript](https://www.youtube.com/watch?v=Nt-qa_LlUH0)
-[Master the JavaScript interview: what is a closure](https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-closure-b2f0d2152b36)
156
+
-[I never understood JavaScript closures](https://medium.com/dailyjs/i-never-understood-javascript-closures-9663703368e8)
107
157
108
158
## 4. Thinking like a programmer II
109
159
110
160
Becoming a good developer doesn't mean being good at any particular programming language: as a matter of fact, the language doesn't matter much.
111
161
112
-
There are only a handful of core concepts
162
+
This is the secret behind being a good developer: if you understand the concept, structure and principles of what makes a software program work, it doesn't matter in what way (the syntax) it's written.
113
163
114
-
This is the secret behind being a good developer: if you understand the concept, structure and principles of what makes a software program work, it doesn't matter in what way (syntax) it's written.
164
+
This is also the reason why most developers, once they've mastered the fundamentals, are able to pick up another language quite easily. It's not because they have good memory; it's because they can recognize the patterns within the language.
115
165
116
166
-[How To Think Like a Programmer](https://www.youtube.com/watch?v=azcrPFhaY9k)
117
167
-[Computer Language Fundamentals: Five Core Concepts](https://blog.upperlinecode.com/computer-language-fundamentals-five-core-concepts-1aa43e929f40)
0 commit comments