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

Skip to content

Commit 339691f

Browse files
authored
Merge pull request HackYourFuture#30 from remarcmij/master
Updates for JavaScript 2 Class 13
2 parents 8ba83f7 + 72babba commit 339691f

File tree

10 files changed

+379
-39
lines changed

10 files changed

+379
-39
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Here you can find course content and homework for the JavaScript 1,2 and 3 modul
1010
|1.|Git Session with Unmesh|[Reading Week 1](/Week1/README.md) | [Homework Week 1](/Week1/MAKEME.md)|
1111
|2.|• Intro JavaScript (What is it, where can you use it for)<br>• [Variables (var, let, const)](fundamentals/variables.md)<br>• [Basic Data types (Strings, Numbers, Arrays, Booleans)](fundamentals/values.md)<br>• [Operators](fundamentals/operators.md)<br>• [Naming conventions](fundamentals/naming_conventions.md)|[Reading Week 2](/Week2/README.md)|[Homework Week 2](/Week2/MAKEME.md)|
1212
|3.|• Git work flow :smiling_imp:<br>• [Advanced data types (objects)](fundamentals/objects.md) <br>• [Conditional execution](fundamentals/conditional_execution.md) <br>• [Statements vs Expressions](fundamentals/statements_expressions.md)<br> • [Loops (for/while)](fundamentals/loops.md)<br>• [Functions](fundamentals/functions.md) <br>• [Scope](fundamentals/scope.md)|[Reading Week 3](/Week3/README.md)|[Homework Week 3](/Week3/MAKEME.md)|
13-
|4.|• Capturing user input <br>• Events<br>• [Basic DOM manipulations (img src, innerHTML)](fundamentals/DOM_manipulation.md)<br>• Code debugging using the browser <br>• [Code commenting](fundamentals/code_commenting.md)<br>• Structuring code files |[Reading Week 4](/Week4/README.md)|[Homework Week 4](/Week4/MAKEME.md)|
13+
|4.|• Capturing user input <br>• Events<br>• [Basic DOM manipulations (img src, innerHTML)](fundamentals/DOM_manipulation.md)<br>• Code debugging using the browser <br>• [Code commenting](fundamentals/code_commenting.md)<br>• Structuring code files<br>• [Code formatting](fundamentals/code_formatting.md) |[Reading Week 4](/Week4/README.md)|[Homework Week 4](/Week4/MAKEME.md)|
1414
|5.|• Functions + JSON/Arrays<br>• [Array Manipulations](fundamentals/array_manipulation.md)<br>• JSON<br>• [Map and filter](fundamentals/map_filter.md)<br>• Arrow functions |[Reading Week 5](/Week5/README.md)|[Homework Week 5](/Week5/MAKEME.md)|
1515
|6.|[Closures](fundamentals/scope_closures_this.md) <br>• Callbacks|[Reading Week 6](/Week6/README.md)|[Homework Week 6](/Week6/MAKEME.md)|
1616
|7.|• Object Oriented Programming <br>• [Promises](fundamentals/promises.md)|[Reading Week 7](/Week7/README.md)|[Homework Week 7](/Week7/MAKEME.md)|

Week3/README.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,39 @@ In week three we will discuss the following topics:
1515
### A Refresher from some previous covered topics:
1616

1717
Links to MDN (Mozilla Developer Network) topics:
18-
- [Arguments object](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/arguments)
18+
1919
- [Strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode)
2020

21-
- If you want to find out more about (mouse) events, check out this resource: https://www.quirksmode.org/js/events_mouse.html
21+
## Review
22+
23+
Go through the topics of week 2-4.
24+
25+
### Week 2
26+
27+
- [Variables (var, let, const)](../fundamentals/variables.md)
28+
- [Basic Data types (Strings, Numbers, Arrays, Booleans)](../fundamentals/values.md)
29+
- [Operators](../fundamentals/operators.md)
30+
- [Naming conventions](../fundamentals/naming_conventions.md)
31+
32+
### Week 3
33+
34+
- [Advanced data types (objects)](../fundamentals/objects.md)
35+
- [Conditional execution](../fundamentals/conditional_execution.md) <br>
36+
- [Statements vs Expressions](../fundamentals/statements_expressions.md)<br>
37+
- [Loops (for/while)](../fundamentals/loops.md)
38+
- [Functions](../fundamentals/functions.md)
39+
- [Scope](../fundamentals/scope.md)
40+
41+
### Week 4
42+
43+
- Capturing user input
44+
- Events
45+
- [Basic DOM manipulations (img src, innerHTML)](../fundamentals/DOM_manipulation.md)
46+
- Code debugging using the browser
47+
- [Code commenting](../fundamentals/code_commenting.md)
48+
- Structuring code files
49+
- [Code formatting](../fundamentals/code_formatting.md)
2250

23-
### Review
24-
- Go through the review of [week 1](https://github.com/HackYourFuture/JavaScript/blob/master/Week1/REVIEW.md)
25-
- Go through the review of [week 2](https://github.com/HackYourFuture/JavaScript/blob/master/Week2/REVIEW.md)
26-
- Go through the review of [week 3](https://github.com/HackYourFuture/JavaScript/blob/master/Week3/REVIEW.md)
27-
<!-- - Go through the review of [week 4](https://github.com/HackYourFuture/JavaScript/blob/master/Week4/REVIEW.md) -->
2851

2952
_Please go through the material and come to class prepared!_
3053

Week4/MAKEME.md

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,33 @@ Topics discussed in class this week:
1717
Give yourself (or your neighbor) a little tap on the shoulder, you've made it to JS2! :muscle:
1818

1919
## Step 0: review
20-
- Go through the review of [the first week](/Week1/REVIEW.md)
21-
- Go through the review of [the second week](/Week2/REVIEW.md)
22-
- Go through the review of [the third week](/Week3/REVIEW.md)
20+
21+
Week 2
22+
23+
- [Variables (var, let, const)](../fundamentals/variables.md)
24+
- [Basic Data types (Strings, Numbers, Arrays, Booleans)](../fundamentals/values.md)
25+
- [Operators](../fundamentals/operators.md)
26+
- [Naming conventions](../fundamentals/naming_conventions.md)
27+
28+
Week 3
29+
30+
- [Advanced data types (objects)](../fundamentals/objects.md)
31+
- [Conditional execution](../fundamentals/conditional_execution.md) <br>
32+
- [Statements vs Expressions](../fundamentals/statements_expressions.md)<br>
33+
- [Loops (for/while)](../fundamentals/loops.md)
34+
- [Functions](../fundamentals/functions.md)
35+
- [Scope](../fundamentals/scope.md)
36+
37+
Week 4
38+
39+
- Capturing user input
40+
- Events
41+
- [Basic DOM manipulations (img src, innerHTML)](../fundamentals/DOM_manipulation.md)
42+
- Code debugging using the browser
43+
- [Code commenting](../fundamentals/code_commenting.md)
44+
- Structuring code files
45+
- [Code formatting](../fundamentals/code_formatting.md)
46+
2347

2448
## Step 1: Implement feedback
2549

@@ -34,7 +58,7 @@ Your fellow students have provided you with feedback in Trello.
3458

3559
_Deadline Monday_
3660

37-
Your Github should contain two repositories called hyf-javascript1 and hyf-commandline. Inside the JavaScript repository you should have three folders, called week1, week2, and week3 (or something similar). Inside these folders you should have the different assignments (a file per exercises). Try and find proper names for the exercises that reflect somehow what is going on in the code. Avoid using spaces in your file names, this makes it harder to "run" you files. Also make sure that all your JavaScript files have a `.js` extension.
61+
Your Github should contain two repositories called `hyf-javascript1` and `hyf-javascript2`. Inside the JavaScript repositories you should have three folders, called week1, week2, and week3 (or something similar). Inside these folders you should have the different assignments (a file per exercises). Try and find proper names for the exercises that reflect somehow what is going on in the code. Avoid using spaces in your file names, this makes it harder to "run" you files. Also make sure that all your JavaScript files have a `.js` extension.
3862

3963
## Step 3: Custom DOM manipulation challenge :mortar_board:
4064

@@ -56,9 +80,9 @@ _Deadline Saturday_
5680

5781
```
5882
How to hand in your homework:
59-
• Upload your homework in your "hyf-javascript1" Github repository. Make sure to create a new folder "week3" first.
60-
• Upload your homework files inside the week3 folder and write a description for this “commit”.
61-
• Your hyf-javascript1/week3 should now contain an index.html, main.css and a script.js file (and the images folder)
83+
• Upload your homework in your "hyf-javascript2" Github repository. Make sure to create a new folder "week1" first.
84+
• Upload your homework files inside the week1 folder and write a description for this “commit”.
85+
• Your hyf-javascript2/week1 should now contain an index.html, main.css and a script.js file (and the images folder)
6286
• Place the link to your repository folder in Trello.
6387
```
6488

fundamentals/code_formatting.md

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
# Code Formatting
2+
3+
When you write your JavaScript code you need to take into account two types of consumer:
4+
5+
1. Human readers (you yourself, your co-workers, classmates, you yourself in a year's time from now, etc.).
6+
2. The JavaScript engine.
7+
8+
Starting with the latter, the JavaScript engine does not care about code formatting at all and is perfectly happy to work with one-letter variable names. 'Minification' is a process that is sometimes used to reduce the size of JavaScript files hosted on a web server so that they take less time to transfer over a network to a web browser. The code below show some 'minified' code.
9+
10+
```js
11+
const o=[6,3,10,1].reduce((o,c)=>(o.push(c*c),o),[]);console.log(o);
12+
```
13+
14+
Clearly, the above code is incomprehensible to humans. The original code is shown below, nicely formatted for the benefit of human readers.
15+
16+
```js
17+
const arr = [6, 3, 10, 1];
18+
const squares = arr.reduce((acc, elem) => {
19+
acc.push(elem * elem);
20+
return acc;
21+
}, []);
22+
console.log(squares); // -> [36, 9, 100, 1]
23+
```
24+
25+
In comparison with the minified code, the original code makes use of new lines and indentation to show structure and uses meaningful variable names, solely for the benefit of the human reader.
26+
27+
Over time, a standard way of formatting JavaScript code has emerged (actually this standard is fairly common across all languages that are derived from the C-language, including JavaScript but also C++, Java and C#).
28+
29+
In the next sections we will discuss the most important code formatting conventions for JavaScript. At the end of this document you will find some code that is formatted according to these rules.
30+
31+
## Blank lines
32+
33+
Use a single blank line to separate blocks of related code. This is similar to separating paragraphs with a blank line in written text.
34+
35+
## Curly braces
36+
37+
Curly braces are used to start and end code blocks, often as part of an `if`, `switch`, `while` or `for` statement. The opening curly brace should be placed at the end of a line. The closing curly brace should be aligned with the beginning of the line that started the code block.
38+
39+
```js
40+
if (condition) {
41+
// ...
42+
} else {
43+
// ...
44+
}
45+
```
46+
47+
## Indentation
48+
49+
Code blocks inside curly braces should be indented by 2 or 4 spaces (choose one or the other, and then stick to it). In case of nested code blocks, for each level of nesting the amount of indentation should be incremented by the standard amount (2 or 4):
50+
51+
```js
52+
function myFunction() {
53+
if (condition) {
54+
// ...
55+
} else {
56+
// ...
57+
}
58+
}
59+
```
60+
61+
## Spacing
62+
63+
- Add a space after keywords, e.g. `if (`, `for (`
64+
- Add a space after a `,` `;` (in a `for` loop) and `:` (e.g. object keys)
65+
- Add a space before and after operators, e.g. `a + b`
66+
67+
## Use VSCode for well-formatted JavaScript
68+
69+
Rather than continuing and specifying every little detail on how to format JavaScript code in a standard format, we can call in the help of VSCode.
70+
71+
VSCode comes with a built-in code formatter for JavaScript. To format the current document in a standard fashion, press the following key combination:
72+
73+
| | Windows | Mac | Linux |
74+
| --------- | ------- | ----- | ----- |
75+
| **Format Document** (make it pretty) | Shift‑Alt‑F | ⇧⌥F| Ctrl‑Shift‑I |
76+
77+
Just make it a habit to bring this three-finger salute just before saving your document or whenever it becomes messy and you're good to go!
78+
79+
> There are a number of user settings that you can apply in VSCode to enable auto-formatting as you type (or paste). See [VSCode Tips](../VSCodeTips/README.md#customise-vscode-settings) for further details.
80+
>
81+
82+
## ESLint
83+
84+
[ESLint](https://eslint.org/) is a tool that can check your JavaScript code for common errors and bad practices. See [VSCode Tips](../VSCodeTips/README.md#installation-instructions) on how to set it up.
85+
86+
ESLint is configured via user-definable rules that specify what to check. These rules must be defined in a file called `.eslintrc.json` placed in the root folder of your project repository. For the lectures and homework of the three HYF JavaScript modules it is recommended that you create an `.eslintrc.json` file and copy and paste the content shown below into that file.
87+
88+
More information about ESLint Rules [here](https://eslint.org/docs/rules/).
89+
90+
Make sure that you correct any errors and warnings that ESLint produces before considering your JavaScript code done!
91+
92+
```json
93+
{
94+
"env": {
95+
"browser": true,
96+
"commonjs": true,
97+
"es6": true,
98+
"node": true
99+
},
100+
"parserOptions": {
101+
"ecmaFeatures": {
102+
"jsx": true
103+
},
104+
"sourceType": "module"
105+
},
106+
"extends": [
107+
"eslint:recommended"
108+
],
109+
"rules": {
110+
"no-const-assign": "warn",
111+
"no-this-before-super": "warn",
112+
"no-undef": "warn",
113+
"no-unreachable": "warn",
114+
"no-unused-vars": "warn",
115+
"constructor-super": "warn",
116+
"valid-typeof": "warn",
117+
"no-var": "warn",
118+
"prefer-const": "warn",
119+
"no-multiple-empty-lines": "warn",
120+
"eol-last": [
121+
"error",
122+
"always"
123+
],
124+
"no-console": "off",
125+
"camelcase": "warn",
126+
"eqeqeq": [
127+
"error",
128+
"always",
129+
{
130+
"null": "ignore"
131+
}
132+
],
133+
"semi": [
134+
"warn",
135+
"always"
136+
]
137+
}
138+
}
139+
```
140+
141+
142+
## Example of well-formatted code
143+
144+
(With the help of the VSCode formatter and ESLint.)
145+
146+
```js
147+
'use strict';
148+
149+
const board = [
150+
['T', 'T', '.', 'F'],
151+
['T', '.', '.', '.'],
152+
['.', '.', '.', '.'],
153+
['R', '.', '.', 'W']
154+
];
155+
156+
const robot = {
157+
x: 0,
158+
y: 0,
159+
dir: 'up',
160+
};
161+
162+
let flagReached = false;
163+
let moves = 0;
164+
165+
board.reverse();
166+
167+
const trailIndicators = {
168+
left: '',
169+
right: '',
170+
up: '',
171+
down: ''
172+
};
173+
174+
function render() {
175+
console.log('\n ' + moves + ':');
176+
for (let row = board.length - 1; row >= 0; row--) {
177+
const cells = board[row];
178+
let line = '';
179+
for (let col = 0; col < cells.length; col++) {
180+
line += ' ' + cells[col] + ' ';
181+
}
182+
console.log(line);
183+
}
184+
if (flagReached) {
185+
console.log('\nHurray! Flag reached in ' + moves + ' steps!');
186+
}
187+
}
188+
189+
function move() {
190+
let x = robot.x;
191+
let y = robot.y;
192+
193+
switch (robot.dir) {
194+
case 'up':
195+
y = y < board.length - 1 ? y + 1 : y;
196+
break;
197+
case 'down':
198+
y = y > 0 ? y - 1 : y;
199+
break;
200+
case 'left':
201+
x = x > 0 ? x - 1 : x;
202+
break;
203+
case 'right':
204+
x = x < board[y].length - 1 ? x + 1 : x;
205+
break;
206+
}
207+
208+
const cellContents = board[y][x];
209+
210+
if (cellContents === '.' || cellContents === 'F') {
211+
board[robot.y][robot.x] = trailIndicators[robot.dir];
212+
robot.x = x;
213+
robot.y = y;
214+
board[y][x] = 'R';
215+
if (cellContents === 'F') {
216+
flagReached = true;
217+
}
218+
}
219+
220+
moves += 1;
221+
render();
222+
}
223+
224+
function turn(turnDirection) {
225+
if (turnDirection !== 'left' && turnDirection !== 'right') {
226+
console.log('ignoring invalid turn', turnDirection);
227+
}
228+
switch (robot.dir) {
229+
case 'up':
230+
robot.dir = turnDirection === 'left' ? 'left' : 'right';
231+
break;
232+
case 'down':
233+
robot.dir = turnDirection === 'left' ? 'right' : 'left';
234+
break;
235+
case 'left':
236+
robot.dir = turnDirection === 'left' ? 'down' : 'up';
237+
break;
238+
case 'right':
239+
robot.dir = turnDirection === 'left' ? 'up' : 'down';
240+
break;
241+
}
242+
}
243+
244+
render();
245+
246+
// start of robot game instructions
247+
move();
248+
turn('right');
249+
move();
250+
move();
251+
move();
252+
turn('left');
253+
move();
254+
move();
255+
// end of robot game instructions
256+
```
257+

0 commit comments

Comments
 (0)