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

Skip to content

Commit 54cdd24

Browse files
committed
corrected bullet list formatting, added semicolons
1 parent e15b715 commit 54cdd24

File tree

1 file changed

+64
-73
lines changed

1 file changed

+64
-73
lines changed

Week3/REVIEW.md

Lines changed: 64 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,75 @@
22

33
```
44
This review covers:
5-
More CLI
6-
Scope, closures and 'this'
7-
Array Manipulations
8-
Basic DOM manipulations
9-
Code commenting
5+
- More CLI
6+
- Scope, closures and 'this'
7+
- Array Manipulations
8+
- Basic DOM manipulations
9+
- Code commenting
1010
```
1111

1212
## More CLI
1313
Check out the CLI review here: https://github.com/HackYourFuture/CommandLine/blob/master/Lecture-2.md
1414

1515
## Scope, closures and 'this'
16-
Scope, closure and 'this' are about *context*.
16+
Scope, closure and 'this' are about *context*.
1717

1818
This post explains things really well: [Recommended read by Todd Motto on Scope](https://toddmotto.com/everything-you-wanted-to-know-about-javascript-scope/)
1919

2020
In this review we won't go over how JavaScript implements scope. We would just be rewriting the above post by Todd Motto.
2121

22-
Instead, let's focus on a couple of important **words** that are used in explaining scope. Understanding the JavaScript side of things can be difficult if we don't fully understand these words.
22+
Instead, let's focus on a couple of important **words** that are used in explaining scope. Understanding the JavaScript side of things can be difficult if we don't fully understand these words.
2323

2424
### How to think about context?
25-
Consider the following sentences:
25+
Consider the following sentences:
2626
> Eyad is a great cook. *He* loves to make ravioli.
2727
28-
> Gijs is a great cook. *He* loves to make ravioli.
28+
> Gijs is a great cook. *He* loves to make ravioli.
2929
3030
In both cases, the second sentence is the exact same. However, the word *he* refers to a completely different person. He is defined by the context.
3131

3232
A second example:
3333
> *He* loves to make ravioli.
3434
35-
Now, when this sentence is written down without *he* being defined in the context, the sentence doesn't make any sense.
35+
Now, when this sentence is written down without *he* being defined in the context, the sentence doesn't make any sense.
3636

3737
### Context in programming:
38-
A JavaScript example:
38+
A JavaScript example:
3939
```js
4040
function myFunction() {
41-
const a = 'ravioli'
42-
console.log(a)
41+
const a = 'ravioli';
42+
console.log(a);
4343
}
4444
```
4545

4646
```js
4747
function myFunction() {
48-
console.log(a)
48+
console.log(a);
4949
}
5050
```
5151

52-
In both cases, `console.log(a)` is the exact same. However, the context defines the value of a and whether it is defined at all.
52+
In both cases, `console.log(a)` is the exact same. However, the context defines the value of a and whether it is defined at all.
5353

54-
### The Scope of the Context
54+
### The Scope of the Context
5555
Let's first look at a definition of `scope`
5656
> (1) the extent of the area or subject matter that something deals with or to which it is relevant.
5757
> (2) the opportunity or possibility to do or deal with something.
5858
5959
So in words more applicable to our situation scope means:
60-
> code that is within the reach of our code.
60+
> code that is within the reach of our code.
6161
6262
Consider two completely different JavaScript files
6363
```js
6464
// aFile.js
65-
const a = 10
65+
const a = 10;
6666
```
6767

6868
```js
6969
// anotherFile.js
70-
console.log(a)
70+
console.log(a);
7171
```
7272

73-
When we run these files separately, the `console.log(a)` statement in anotherFile.js of course cannot reach `var a = 10`. Why? It is beyond the scope of our context. When we run a file in JavaScript, the program creates a new top-level context we call the global scope.
73+
When we run these files separately, the `console.log(a)` statement in anotherFile.js of course cannot reach `const a = 10`. Why? It is beyond the scope of our context. When we run a file in JavaScript, the program creates a new top-level context we call the global scope.
7474

7575
From now on, we'll call 'scoped context' simply 'scope'.
7676

@@ -82,20 +82,20 @@ At school one learns that *he* will refer to the last masculine subject introduc
8282

8383
There are *five different ways* we can create a new context in JavaScript:
8484
- The global context (as we already saw)
85-
- The simple function context
86-
- The object construction context
85+
- The simple function context
86+
- The object construction context
8787
- The object method context
8888
- The event listener context
8989

9090
More info on this [in this great post](https://zellwk.com/blog/this/)
9191

9292
## Array Manipulation
9393
[MDN on Arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
94-
As we know by now, arrays are collections of values.
94+
As we know by now, arrays are collections of values.
9595

96-
As we will see, there are often many ways to achieve the same thing when working arrays. Over time, you will add different techniques to your mental toolbox to achieve the result you want quickly.
96+
As we will see, there are often many ways to achieve the same thing when working arrays. Over time, you will add different techniques to your mental toolbox to achieve the result you want quickly.
9797

98-
The basics.
98+
The basics.
9999

100100
- How do we create an array?
101101
- How do we add items to an array?
@@ -106,98 +106,98 @@ The basics.
106106

107107

108108
### How do we create an array?
109-
An array can be created multiple ways
109+
An array can be created multiple ways
110110

111-
From scratch:
111+
From scratch:
112112
```js
113-
const a = [] // result: []
114-
const b = ['item1', 'item2'] // result: ['item1', 'item2']
115-
const c = new Array() // result: []
116-
const d = new Array('item 1', 'item2') // result: ['item1', 'item2']
117-
const e = new Array(20) // result: [ <20 empty items> ]
118-
const f = new Array(20, 21) // result: [20, 21]
113+
const a = []; // result: []
114+
const b = ['item1', 'item2']; // result: ['item1', 'item2']
115+
const c = new Array(); // result: []
116+
const d = new Array('item 1', 'item2'); // result: ['item1', 'item2']
117+
const e = new Array(20); // result: [ <20 empty items> ]
118+
const f = new Array(20, 21); // result: [20, 21]
119119
// Note that `e` and `f` are a beautiful example of how weird and unexpected JavaScript can be. You will probably use `a` most often.
120120
```
121121

122122
From value (as an example, many ways to create an array from another value):
123123
```js
124-
const a = 'hello world' // result: 'hello world'
125-
const b = a.split(' ') // result: ['hello', 'world' ]
124+
const a = 'hello world'; // result: 'hello world'
125+
const b = a.split(' '); // result: ['hello', 'world' ]
126126
```
127127

128128
### Array length
129129
Every array has as a 'static' property `length`. Meaning that we can easily get the amount of items in an array.
130130
```js
131-
const f = ['hi','there']
132-
console.log(f.length) // 2
131+
const f = ['hi','there'];
132+
console.log(f.length); // 2
133133
```
134134

135135
### Array index
136136
We can access array elements through the position of the element in the array. This is called an index. Indices (plural of index) are 0-based, meaning that the first item's index is 0, the second element is 1.
137137

138138
```js
139-
const x = ['first', 'second', 'third']
140-
console.log(x[0]) // 'first'
139+
const x = ['first', 'second', 'third'];
140+
console.log(x[0]); // 'first'
141141

142-
x[3] = 'fourth'
142+
x[3] = 'fourth';
143143
```
144144

145-
Note that arrays can have empty values. This should be avoided usually to prevent unexpected behaviour.
145+
Note that arrays can have empty values. This should be avoided usually to prevent unexpected behaviour.
146146
```js
147-
x[10] = 'eleventh'
148-
console.log(x) // [ 'first', 'second', 'third', 'fourth', <6 empty items>, 'eleventh' ]
147+
x[10] = 'eleventh';
148+
console.log(x); // [ 'first', 'second', 'third', 'fourth', <6 empty items>, 'eleventh' ]
149149
```
150150

151151
Next to the index, we have a wide range of tools to manipulate arrays.
152152

153153
### Array methods
154-
These methods are essential.
154+
These methods are essential.
155155

156156
**Extremely important is to remember to always ask these two questions**:
157-
What is the return value of this method?
158-
What does this method do to the original array it is used on?
157+
- What is the return value of this method?
158+
- What does this method do to the original array it is used on?
159159

160160
**Adding items**
161-
[`.push()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) Add item to end of array
162-
[`.unshift()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift) Add item to beginning of array
161+
- [`.push()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) Add item to end of array
162+
- [`.unshift()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift) Add item to beginning of array
163163

164164
**Removing items**
165-
[`.shift()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) Remove first element from array
166-
[`.pop()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop) Remove last element from array
167-
[`.splice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) Remove a specific element from array using index
165+
- [`.shift()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) Remove first element from array
166+
- [`.pop()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop) Remove last element from array
167+
- [`.splice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) Remove a specific element from array using index
168168

169169
**Useful iterators over arrays**
170-
[`.map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) creates a new array with the results of calling a provided function on every element in the calling array.
171-
[`.filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) creates a new array with all elements that pass the test implemented by the provided function.
172-
[`.sort()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) sorts the elements of an array in place and returns the array
170+
- [`.map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) creates a new array with the results of calling a provided function on every element in the calling array.
171+
- [`.filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) creates a new array with all elements that pass the test implemented by the provided function.
172+
- [`.sort()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) sorts the elements of an array in place and returns the array
173173

174174

175175
## Basic DOM manipulations
176-
Using JavaScript we can access and manipulate the Document Object Model (DOM). We access the DOM through a global object called `document`.
176+
Using JavaScript we can access and manipulate the Document Object Model (DOM). We access the DOM through a global object called `document`.
177177

178-
HTML
178+
HTML
179179
```html
180180
<body>
181-
<div id="hello"></div>
181+
<div id="hello"></div>
182182
</body>
183183
```
184184

185185
A common method to access the DOM is by giving a HTML element an ID, and then using the `document` method `getElementById()`
186186

187187
```js
188-
const x = document.getElementById('hello')
188+
const x = document.getElementById('hello');
189189
```
190190

191191
Now we have stored a *reference* of how that HTML element is accessed through the DOM object. We can use this to manipulate the element.
192192

193193
```js
194-
x.innerHTML = 'hello'
194+
x.innerHTML = 'hello';
195195
```
196196

197197
We can also create elements
198198
```js
199-
const a = document.createElement('li')
200-
x.appendChild(a)
199+
const a = document.createElement('li');
200+
x.appendChild(a);
201201
```
202202

203203

@@ -223,7 +223,7 @@ Coding **well** in JavaScript: [JSDoc](http://usejsdoc.org/)
223223
[W3Schools](https://www.w3schools.com/html/html_comments.asp)
224224
Comments
225225
```html
226-
<!-- Write
226+
<!-- Write
227227
your comments here -->
228228

229229
<!-- Write your comments here -->
@@ -244,15 +244,6 @@ lines
244244
```
245245

246246
### When to comment?
247-
Now for the hard part: when to comment? When you work for different companies, you will see different styles. Embrace something you like, and then learn to let go. Google on "when to comment code?" and you'll find a big bunch of different opinions.
248-
249-
The general concept is, however, that it is there to help make the code more easy to understand. Note, however, that comments can also make code more difficult to understand when not applied properly.
250-
251-
252-
253-
254-
255-
256-
257-
247+
Now for the hard part: when to comment? When you work for different companies, you will see different styles. Embrace something you like, and then learn to let go. Google on "when to comment code?" and you'll find a big bunch of different opinions.
258248

249+
The general concept is, however, that it is there to help make the code more easy to understand. Note, however, that comments can also make code more difficult to understand when not applied properly.

0 commit comments

Comments
 (0)