Skip to content

Commit 8f90c17

Browse files
committed
Improve examples
1 parent 1f4a7b8 commit 8f90c17

File tree

7 files changed

+37
-11
lines changed

7 files changed

+37
-11
lines changed

JavaScript/4-for-in.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
'use strict';
22

33
const numbers = [7, 10, 1, 5, 2];
4-
numbers.field = 'Value';
4+
numbers.field2 = 'Value2';
5+
numbers[-10] = 'Value3';
6+
numbers.field1 = 'Value1';
7+
numbers[5] = 20;
58

69
for (const i in numbers) {
710
const value = numbers[i];
8-
console.log(i, value);
11+
console.log(i, typeof(i), value);
912
}
1013

1114
let j, value;

JavaScript/5-for-of.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const numbers = [7, 10, 1, 5, 2];
44
numbers.field = 'Value';
5+
numbers[7] = 5;
56

67
for (const i of numbers) {
78
console.log(i);

JavaScript/6-break.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const flag = true;
3+
const flag = false;
44

55
label1: {
66
console.log('Hello');
@@ -9,5 +9,7 @@ label1: {
99
label2: {
1010
console.log('There');
1111
break label2;
12+
console.log('Bye');
1213
}
14+
console.log('End');
1315
}

JavaScript/7-continue.js

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

33
let i = 0;
44

5-
label1: while (i < 10) {
5+
while (i < 10) {
66
i++;
77
console.log('Hello');
8-
if (i === 5) continue label1;
8+
if (i === 5) continue;
99
console.log('World');
1010
}

JavaScript/8-forEach.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const numbers = [7, 10, 1, 5, 2];
4-
54
numbers.forEach((item, i, arr) => {
65
console.log(i, arr, item);
76
});
@@ -12,4 +11,6 @@ numbers.forEach((item, i, arr) => {
1211

1312
[7, 10, 1].forEach(x => console.log(x));
1413

15-
[7, 10, 1].forEach(console.log);
14+
const log = x => console.log(x);
15+
16+
[7, 10, 1].forEach(log);

JavaScript/9-map.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
'use strict';
22

3-
const log = s => {
4-
console.log(s);
3+
const log = (s, i) => {
4+
console.log(i, s);
5+
return s;
56
};
67

7-
[7, 10, 1, 5, 2].map(x => x * 2).map(log);
8+
const f1 = x => x * 2;
9+
const f2 = x => ++x;
10+
11+
const compose = (...funcs) => x => funcs.reduce((v, f) => f(v), x);
12+
13+
const f3 = compose(f1, f2);
14+
15+
const res1 = [7, 10, 1, 5, 2]
16+
.filter(x => x > 4)
17+
.map(f3)
18+
.reduce((acc, val) => acc + val);
19+
console.log(res1);
20+
21+
[7, 10, 1, 5, 2]
22+
.map(log)
23+
.map(x => x * 2)
24+
.map(log)
25+
.map(x => ++x)
26+
.map(log);

JavaScript/a-matrix.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const matrix = [
77
[-6, 4, 8, 2, 0],
88
];
99

10-
const max = (a, b) => a > b ? a : b;
10+
const max = (a, b) => (a > b ? a : b);
1111

1212
const res = matrix
1313
.map(row => row.reduce(max))

0 commit comments

Comments
 (0)