Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e215721

Browse files
author
Noer Paanakker
committed
finished reading week 2/3
1 parent fe44811 commit e215721

File tree

6 files changed

+96
-64
lines changed

6 files changed

+96
-64
lines changed

Week1/MAKEME.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
## **1. Practice the concepts**
1111

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:
1313

1414
- [Making webpages interactive](https://www.khanacademy.org/computing/computer-programming/html-css-js)
1515

1616
## **2. JavaScript exercises**
1717

1818
> 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`.
1919
20-
### 1. The book list
20+
**Exercise 1: The book list**
2121

2222
I'd like to display my three favorite books inside a nice webpage!
2323

@@ -42,7 +42,7 @@ const books = [
4242
4. Add an <img> to each book that links to a URL of the book cover.
4343
5. Change the style of the book depending on whether you have read it (green) or not (red).
4444

45-
### 2. About me
45+
**Exercise 2: About me**
4646

4747
Start with this HTML and save it as "about_me.html":
4848

@@ -72,7 +72,7 @@ Start with this HTML and save it as "about_me.html":
7272
5. (In the HTML head) Add a style tag that sets a rule for .list-item to make the color red.
7373
6. Create a new img element and set its src attribute to a picture of you. Append that element to the page.
7474

75-
### 3. The logo hijack
75+
**Exercise 3: The logo hijack**
7676

7777
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.
7878

@@ -82,7 +82,7 @@ In this exercise you're expected to write a JavaScript function that can be exec
8282
2. Find out how to select the element that contains the Google logo, and store it in a variable
8383
3. Modify the source and sourceset of the logo so that it's replaced by the HackYourFuture logo instead
8484

85-
### 4. What's the time?
85+
**Exercise 4: What's the time?**
8686

8787
Why wear a watch when you can check the time, live in your webpage?
8888

@@ -91,7 +91,7 @@ Why wear a watch when you can check the time, live in your webpage?
9191
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
9292
4. Have the function execute when it's loading in the browser
9393

94-
### 5. The cat walk
94+
**Exercise 5: The cat walk**
9595

9696
Start with this webpage, which has a single img tag of an animated GIF of a cat walking.
9797

Week1/README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ The actual process of transforming HTML, CSS and JavaScript into a user-viewable
9292

9393
## 3. What is DOM Manipulation?
9494

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.
9996

10097
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).
10198

@@ -156,8 +153,8 @@ Test this code out by copying and pasting it in the Developer Console of your br
156153
This is DOM manipulation in its simplest form. It goes in three essential steps:
157154

158155
(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)
161158

162159
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`).
163160

Week2/MAKEME.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const mondayTasks = [
6969

7070
Let's assume your hourly rate is €25. How much would you earn on that day?
7171

72-
- Write a program that computes that:
72+
- Write a program that finds out what your hourly rate on a Monday would be
7373
- Use the `map` array function to take out the duration time for each task.
7474
- Multiply each duration by a per-hour rate for billing and sum it all up.
7575
- Output a formatted Euro amount, rounded to Euro cents, e.g: `€11.34`.

Week2/README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ This example illustrates the concept of **asynchronicity**: there are multiple p
8080

8181
This is the utility of `callbacks`: they allow us to introduce asynchronicity into the control flow of an application.
8282

83-
Let's put this into the context of a webpage.
84-
8583
Study the following resources to learn more about the importance of callbacks:
8684

8785
- [Asynchronous JavaScript](https://www.youtube.com/watch?v=YxWMxJONp7E)
@@ -90,8 +88,6 @@ Study the following resources to learn more about the importance of callbacks:
9088

9189
## 3. Array Functions
9290

93-
In programming we're always trying to make things easier on ourselves.
94-
9591
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.
9692

9793
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
151147
1. Heap. This is where the browser assigns space in memory to each process
152148
2. Call Stack. This is the amount of JavaScript commands (read: function calls and events) that need to be executed
153149
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
155151

156152
To see it in action check out the following resources:
157153

Week3/MAKEME.md

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ letters === ['a', 'b', 'c', 'd', 'e', 'f'];
5656

5757
**Exercise 3: Guess the output**
5858

59-
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.
6060

6161
```js
62+
// Snippet
6263
let a = 10;
6364
const x = (function() {
6465
a = 12;
@@ -70,7 +71,12 @@ const x = (function() {
7071
x();
7172
```
7273

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+
7378
```js
79+
// Snippet
7480
const x = 9;
7581
function f1(val) {
7682
val = val + 1;
@@ -88,25 +94,6 @@ f2(y);
8894
console.log(y);
8995
```
9096

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-
11097
**Exercise 5: The lottery machine**
11198

11299
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
126113

127114
Both functions should be called if the array value is divisible by both 3 and 5.
128115

116+
Here is a starter template:
117+
129118
```js
130119
function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
131120
const numbers = [];

Week3/README.md

Lines changed: 76 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ These are the topics for week 3:
88
- Global vs. local (function and block)
99
- Const and let
1010
2. Hoisting
11-
- What is it
12-
- When does it happen
13-
- Why do we need to know it?
11+
- When does hoisting happen?
1412
3. Closures
1513
- Execution context
16-
- Defining a function within a function
14+
- Why do we need closures?
1715
4. Thinking like a programmer II
1816

1917
## 1. Scope
@@ -36,34 +34,69 @@ It depends on `context` for me to know what really happened.
3634

3735
Let's draw the line to programming. Simply put, just like context gives meaning to a word, `scope` gives meaning to a variable/object.
3836

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.
4038

4139
For further research, check out the following:
4240

4341
- [Variable Scope & Context](https://www.youtube.com/watch?v=WPcW83BMT3Y)
4442

4543
### Global vs. local (function and block)
4644

47-
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.
4846

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.
5148

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.
5350

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+
function createLocalScope() {
53+
const localVariable = 'this variable can only be accessed within this function';
54+
console.log(localVariable);
55+
56+
const localOnlyHere = 'This variable can only be accessed here, nowhere else';
57+
}
58+
59+
function createAnotherLocalScope() {
60+
const localVariable =
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+
```
5569

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.
5771

5872
```js
59-
var myName = 'Mohammed';
73+
const globalVariable = 'This variable can be accessed wherever in the code';
74+
75+
function accessGlobalVariable() {
76+
console.log(globalVariable);
77+
}
78+
console.log(globalVariable);
79+
accessGlobalVariable();
6080
```
6181

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
6391

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:
6597

6698
- [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)
67100

68101
## 2. Hoisting
69102

@@ -73,45 +106,62 @@ If you look up the term "hoisting" in any dictionary, you'll find something like
73106
74107
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.
75108

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.
77110

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.
79112

80-
### When does it happen
113+
### When does hoisting happen?
81114

82-
### Why do we need to know it?
115+
Hoisting happens during `compile-time`.
83116

84117
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.
85118

86119
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.
87120

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)
89124

90125
## 3. Closures
91126

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.
93130

94131
### Execution context
95132

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:
97146

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.
99148
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.
101150

102151
For further study please check the following resources:
103152

104153
- [The Ultimate Guide to Execution Contexts, Hoisting, Scoping and Closures in JavaScript](https://www.youtube.com/watch?v=Nt-qa_LlUH0)
105154
- [Understanding Closures](https://www.youtube.com/watch?v=rBBwrBRoOOY)
106-
- [Let's Learn JavaScript Closures](https://www.freecodecamp.org/news/lets-learn-javascript-closures-66feb44f6a44/)
155+
- [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)
107157

108158
## 4. Thinking like a programmer II
109159

110160
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.
111161

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.
113163

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.
115165

116166
- [How To Think Like a Programmer](https://www.youtube.com/watch?v=azcrPFhaY9k)
117167
- [Computer Language Fundamentals: Five Core Concepts](https://blog.upperlinecode.com/computer-language-fundamentals-five-core-concepts-1aa43e929f40)

0 commit comments

Comments
 (0)