Skip to content

Commit 8cf2152

Browse files
committedSep 24, 2017
chapter 02 - arrays
1 parent 6a4eb60 commit 8cf2152

16 files changed

+407
-281
lines changed
 

‎examples/chapter02/01-Introduction.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ averageTemp[2] = 42.4;
1313
averageTemp[3] = 52;
1414
averageTemp[4] = 60.8;
1515

16-
console.log(averageTempJan);
17-
console.log(averageTempFeb);
18-
console.log(averageTempMar);
19-
console.log(averageTempApr);
20-
console.log(averageTempMay);
16+
console.log('averageTempJan', averageTempJan);
17+
console.log('averageTempFeb', averageTempFeb);
18+
console.log('averageTempMar', averageTempMar);
19+
console.log('averageTempApr', averageTempApr);
20+
console.log('averageTempMay', averageTempMay);
2121

22-
console.log(averageTemp[0]);
23-
console.log(averageTemp[1]);
24-
console.log(averageTemp[2]);
25-
console.log(averageTemp[3]);
26-
console.log(averageTemp[4]);
22+
console.log('averageTemp[0]', averageTemp[0]);
23+
console.log('averageTemp[1]', averageTemp[1]);
24+
console.log('averageTemp[2]', averageTemp[2]);
25+
console.log('averageTemp[3]', averageTemp[3]);
26+
console.log('averageTemp[4]', averageTemp[4]);

‎examples/chapter02/02-CreatingAndInitialingArrays.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ daysOfWeek = [];
1212

1313
daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; // {3}
1414

15-
console.log(daysOfWeek.length);
15+
console.log('daysOfWeek.length', daysOfWeek.length);
1616

1717
for (let i = 0; i < daysOfWeek.length; i++) {
18-
console.log(daysOfWeek[i]);
18+
console.log(`daysOfWeek[${i}]`, daysOfWeek[i]);
1919
}
2020

21-
// console.table(daysOfWeek);
22-
2321
// fibonacci numbers
2422
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
2523
const fibonacci = []; // {1}
@@ -31,8 +29,8 @@ for (let i = 3; i < 20; i++) {
3129
}
3230

3331
for (let i = 1; i < fibonacci.length; i++) { // {5}
34-
console.log(fibonacci[i]); // {6}
32+
console.log(`fibonacci[${i}]`, fibonacci[i]); // {6}
3533
}
3634

3735
// instead of {5} and {6} we can simply use
38-
console.log(fibonacci);
36+
console.log('fibonacci', fibonacci);
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,105 @@
11
// @ts-check
2+
/* eslint-disable */
23

3-
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
4+
function logArray(message = '', array) {
5+
const output = `${message} => (${array.length}) [${array.join(', ')}]`;
6+
document.writeln(`<p>${output}</p>`);
7+
console.log(message, array);
8+
}
9+
10+
let numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
411

12+
console.log('Original array', numbers);
513

614
function printArray(myArray) {
715
for (let i = 0; i < myArray.length; i++) {
816
console.log(myArray[i]);
917
}
1018
}
1119

20+
/****************************** Inserting an element in the end of the Array */
21+
1222
// add a new element to the numbers array
1323
numbers[numbers.length] = 10;
24+
console.log('Add 10 to the end', numbers);
1425

1526
numbers.push(11);
27+
console.log('Add 11 with push', numbers);
1628

1729
numbers.push(12, 13);
30+
console.log('Add 12 and 13 with push', numbers);
1831

19-
printArray(numbers);
2032

2133
// insert first position manually
22-
for (let i = numbers.length; i >= 0; i--) {
23-
numbers[i] = numbers[i - 1];
24-
}
25-
26-
numbers[0] = -1;
34+
Array.prototype.insertFirstPosition = function(value) {
35+
for (let i = this.length; i >= 0; i--) {
36+
this[i] = this[i - 1];
37+
}
38+
this[0] = value;
39+
};
2740

28-
printArray(numbers);
41+
numbers.insertFirstPosition(-1);
42+
console.log('Add -1 with insertFirstPosition', numbers);
43+
// printArray(numbers);
2944

3045
// using method unshift
3146
numbers.unshift(-2);
32-
33-
printArray(numbers);
47+
console.log('Add -2 with unshift', numbers);
48+
//printArray(numbers);
3449

3550
numbers.unshift(-4, -3);
51+
console.log('Add -4 and -3 with unshift', numbers);
52+
// printArray(numbers);
3653

37-
printArray(numbers);
38-
39-
// **** Removing elements
54+
// ************** Removing elements
4055

4156
numbers.pop();
57+
console.log('Removed last value with pop', numbers);
4258

43-
// remove first position manually
4459
for (let i = 0; i < numbers.length; i++) {
4560
numbers[i] = numbers[i + 1];
4661
}
4762

48-
printArray(numbers);
63+
console.log('Removed first value manually', numbers);
64+
console.log('Lenght after value removed manually', numbers.length);
65+
66+
//reset array
67+
numbers = [-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
4968

50-
console.log(numbers.length);
69+
Array.prototype.reIndex = function(myArray) {
70+
const newArray = [];
71+
for(let i = 0; i < myArray.length; i++ ) {
72+
if (myArray[i] !== undefined) {
73+
// console.log(myArray[i]);
74+
newArray.push(myArray[i]);
75+
}
76+
}
77+
return newArray;
78+
}
79+
80+
// remove first position manually and reIndex
81+
Array.prototype.removeFirstPosition = function() {
82+
for (let i = 0; i < this.length; i++) {
83+
this[i] = this[i + 1];
84+
}
85+
return this.reIndex(this);
86+
};
87+
88+
numbers = numbers.removeFirstPosition();
89+
console.log('Removed first with removeFirstPosition + reIndex', numbers);
5190

5291
// using method shift
5392
numbers.shift();
54-
55-
printArray(numbers);
56-
console.log(numbers.length);
93+
console.log('Removed first with shift', numbers);
94+
console.log('Lenght after removed first with shift', numbers.length);
5795

5896
//* *** Removing and Adding elements from the middle of the array or specific position
5997
// splice method - parameter (index, howManyPositionsToBeRemoved, item1...itemX)
6098
numbers.splice(5, 3);
61-
62-
console.log('----');
63-
64-
printArray(numbers);
99+
console.log('Removing 3 elements (3, 4, 5) starting index 5', numbers);
65100

66101
numbers.splice(5, 0, 2, 3, 4);
67-
68-
console.log('----');
69-
70-
printArray(numbers);
71-
72-
console.log('----');
102+
console.log('Adding 3 elements (2, 3, 4) starting index 5', numbers);
73103

74104
numbers.splice(5, 3, 2, 3, 4);
75-
76-
printArray(numbers);
105+
console.log('Removing 3 elements starting index 5 and adding (2, 3, 4)', numbers);

‎examples/chapter02/04-TwoDimensionalMultiDimensional.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let averageTemp = [];
88

99
// same as
1010
averageTemp[0] = [72, 75, 79, 79, 81, 81];
11-
averageTemp[1] = [81, 79, 75, 75, 73, 72];
11+
averageTemp[1] = [81, 79, 75, 75, 73, 73];
1212

1313
function printMatrix(myMatrix) {
1414
for (let i = 0; i < myMatrix.length; i++) {
@@ -18,7 +18,8 @@ function printMatrix(myMatrix) {
1818
}
1919
}
2020

21-
printMatrix(averageTemp);
21+
// printMatrix(averageTemp);
22+
console.log('averageTemp two-dimensional array:');
2223

2324
// same as
2425

@@ -37,9 +38,10 @@ averageTemp[1][1] = 79;
3738
averageTemp[1][2] = 75;
3839
averageTemp[1][3] = 75;
3940
averageTemp[1][4] = 73;
40-
averageTemp[1][5] = 72;
41+
averageTemp[1][5] = 73;
4142

42-
printMatrix(averageTemp);
43+
// printMatrix(averageTemp);
44+
console.table(averageTemp);
4345

4446
//* * Multidimensional Matrix
4547

@@ -63,3 +65,14 @@ for (let i = 0; i < matrix3x3x3.length; i++) {
6365
}
6466
}
6567
}
68+
69+
// user-friendly-output
70+
const matrix3x3x3Output = [];
71+
for (let i = 0; i < 3; i++) {
72+
matrix3x3x3Output[i] = [];
73+
for (let j = 0; j < 3; j++) {
74+
matrix3x3x3Output[i][j] = `[${matrix3x3x3[i][j].join(', ')}]`;
75+
}
76+
}
77+
console.log('matrix3x3x3 three-dimensional array:');
78+
console.table(matrix3x3x3Output);

‎examples/chapter02/05-ArrayMethods.js

+29-105
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,47 @@
11
// @ts-check
22

3-
//* ** contact
4-
var zero = 0;
5-
var positiveNumbers = [1, 2, 3];
6-
var negativeNumbers = [-3, -2, -1];
7-
var numbers = negativeNumbers.concat(zero, positiveNumbers);
8-
9-
console.log(numbers);
10-
11-
// console.log(numbers);
12-
13-
//* ** every and some
14-
var isEven = function(x) {
3+
//* ************** Joining multiple arrays
4+
const zero = 0;
5+
const positiveNumbers = [1, 2, 3];
6+
const negativeNumbers = [-3, -2, -1];
7+
let numbers = negativeNumbers.concat(zero, positiveNumbers);
8+
9+
console.log('zero', zero);
10+
console.log('positiveNumbers', positiveNumbers);
11+
console.log('negativeNumbers', negativeNumbers);
12+
console.log('negativeNumbers.concat(zero, positiveNumbers)', numbers);
13+
14+
/* function isEven(x) {
1515
// returns true if x is a multiple of 2.
1616
console.log(x);
17-
return x % 2 === 0;
18-
};
19-
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
17+
return x % 2 === 0 ? true : false;
18+
} */ // ES5 syntax
19+
const isEven = x => x % 2 === 0;
2020

21-
numbers.every(isEven); // is going to execute the function only once
21+
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
22+
console.log('numbers', numbers);
2223

23-
console.log('---');
24+
// it is going to execute the function only once
25+
console.log('numbers.every(isEven)', numbers.every(isEven));
2426

25-
numbers.some(isEven); // is going to execute the function twice
27+
// is going to execute the function twice
28+
console.log('numbers.some(isEven)', numbers.some(isEven));
2629

27-
numbers.forEach(function(x) {
30+
/* numbers.forEach(function(x) {
2831
console.log(x % 2 == 0);
29-
});
32+
}); */ // ES5 sintax for function below
3033

31-
console.log(numbers.reverse());
34+
numbers.forEach(x => console.log(`numbers.forEach: ${x} % 2 === 0`, x % 2 === 0));
3235

33-
// console.log('*** filter ***');
3436

35-
var evenNumbers = numbers.filter(isEven);
37+
console.log('numbers.map(isEven)', numbers.map(isEven));
3638

37-
// console.log(evenNumbers);
39+
console.log('numbers.filter(isEven)', numbers.filter(isEven));
3840

39-
console.log('*** map ***');
40-
41-
// console.log(numbers.map(isEven));
42-
43-
console.log(
41+
/* console.log('numbers.reduce',
4442
numbers.reduce(function(previous, current, index) {
4543
return previous + current;
4644
})
47-
);
48-
49-
console.log(numbers.sort());
50-
51-
console.log(
52-
numbers.sort(function(a, b) {
53-
return a - b;
54-
})
55-
);
56-
57-
function compare(a, b) {
58-
if (a < b) {
59-
return -1;
60-
}
61-
if (a > b) {
62-
return 1;
63-
}
64-
// a must be equal to b
65-
return 0;
66-
}
67-
68-
console.log(numbers.sort(compare));
69-
70-
// Sorting objects
71-
72-
var friends = [
73-
{ name: 'John', age: 30 },
74-
{ name: 'Ana', age: 20 },
75-
{ name: 'Chris', age: 25 }
76-
];
77-
78-
function comparePerson(a, b) {
79-
if (a.age < b.age) {
80-
return -1;
81-
}
82-
if (a.age > b.age) {
83-
return 1;
84-
}
85-
return 0;
86-
}
87-
88-
console.log(friends.sort(comparePerson));
89-
90-
var names = ['Ana', 'ana', 'john', 'John'];
91-
console.log(names.sort());
92-
93-
console.log(
94-
names.sort(function(a, b) {
95-
if (a.toLowerCase() < b.toLowerCase()) {
96-
return -1;
97-
}
98-
if (a.toLowerCase() > b.toLowerCase()) {
99-
return 1;
100-
}
101-
return 0;
102-
})
103-
);
104-
105-
var names2 = ['Maève', 'Maeve'];
106-
console.log(
107-
names2.sort(function(a, b) {
108-
return a.localeCompare(b);
109-
})
110-
);
111-
112-
//* ** toString
113-
console.log(numbers.toString());
114-
115-
console.log(numbers.indexOf(10));
116-
console.log(numbers.indexOf(100));
117-
118-
numbers.push(10);
119-
console.log(numbers.lastIndexOf(10));
120-
console.log(numbers.lastIndexOf(100));
45+
); */ // ES5 sintax for function below
12146

122-
var numbersString = numbers.join('-');
123-
console.log(numbersString);
47+
console.log('numbers.reduce', numbers.reduce((previous, current) => previous + current));
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="06-ES2015Methods.js"></script>
9+
</body>
10+
</html>
+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// @ts-check
2+
3+
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
4+
5+
//* ********* using for..of loop
6+
for (const n of numbers) {
7+
console.log(`for..of loop: ${n} % 2 === 0`, n % 2 === 0 ? 'even' : 'odd');
8+
}
9+
10+
//* ********* Using the new ES6 iterator (@@iterator)
11+
console.log('Using the new ES6 iterator (@@iterator)');
12+
13+
let iterator = numbers[Symbol.iterator]();
14+
console.log('iterator.next().value', iterator.next().value); // 1
15+
console.log('iterator.next().value', iterator.next().value); // 2
16+
console.log('iterator.next().value', iterator.next().value); // 3
17+
console.log('iterator.next().value', iterator.next().value); // 4
18+
console.log('iterator.next().value', iterator.next().value); // 5
19+
20+
// or use code below
21+
iterator = numbers[Symbol.iterator]();
22+
for (const n of iterator) {
23+
console.log(`${n} of iterator`, n);
24+
}
25+
26+
//* ********* Array entries, keys and values
27+
console.log('Array entries, keys and values');
28+
29+
console.log('Array.entries');
30+
let aEntries = numbers.entries(); // retrieve iterator of key/value
31+
console.log('aEntries.next().value', aEntries.next().value); // [0, 1] - position 0, value 1
32+
console.log('aEntries.next().value', aEntries.next().value); // [1, 2] - position 1, value 2
33+
console.log('aEntries.next().value', aEntries.next().value); // [2, 3] - position 2, value 3
34+
35+
// or use code below
36+
aEntries = numbers.entries();
37+
for (const n of aEntries) {
38+
console.log(`entry of ${n}`, n);
39+
}
40+
41+
console.log('Array.keys');
42+
const aKeys = numbers.keys(); // retrieve iterator of keys
43+
console.log('aKeys.next()', aKeys.next()); // {value: 0, done: false } done false means iterator has more values
44+
console.log('aKeys.next()', aKeys.next()); // {value: 1, done: false }
45+
console.log('aKeys.next()', aKeys.next()); // {value: 2, done: false }
46+
47+
console.log('Array.values - Only Edge and Safari - Oct 2017');
48+
// const aValues = numbers.values();
49+
// console.log(aValues.next()); // {value: 1, done: false } done false means iterator has more values
50+
// console.log(aValues.next()); // {value: 2, done: false }
51+
// console.log(aValues.next()); // {value: 3, done: false }
52+
53+
//* ********* Using the from method
54+
console.log('Using the from method');
55+
56+
const evens = Array.from(numbers, x => x % 2 === 0);
57+
console.log('Array.from(numbers, x => x % 2 === 0)', evens);
58+
59+
const numbers2 = Array.from(numbers);
60+
console.log('Array.from(numbers)', numbers2);
61+
62+
//* ********* Using Array.of
63+
console.log('Using Array.of');
64+
65+
const numbers3 = Array.of(1);
66+
const numbers4 = Array.of(1, 2, 3, 4, 5, 6);
67+
const numbersCopy = Array.of(...numbers4);
68+
console.log('Array.of(1)', numbers3);
69+
console.log('Array.of(1, 2, 3, 4, 5, 6)', numbers4);
70+
console.log('Array.of(...numbers4)', numbersCopy);
71+
72+
//* ********* Using the fill method
73+
console.log('Using the fill method');
74+
75+
numbersCopy.fill(0);
76+
console.log('numbersCopy.fill(0)', numbersCopy);
77+
78+
numbersCopy.fill(2, 1);
79+
console.log('numbersCopy.fill(2, 1)', numbersCopy);
80+
81+
numbersCopy.fill(1, 3, 5);
82+
console.log('numbersCopy.fill(1, 3, 5)', numbersCopy);
83+
84+
const ones = Array(6).fill(1);
85+
console.log('Array(6).fill(1)', ones);
86+
87+
//* ********* Using the copyWithin method
88+
console.log('Using the copyWithin method');
89+
90+
let copyArray = [1, 2, 3, 4, 5, 6];
91+
console.log('copyArray', copyArray);
92+
93+
copyArray = copyArray.copyWithin(0, 3); // pos 3 value is copied to pos 0
94+
console.log('copyArray.copyWithin(0, 3)', copyArray);
95+
96+
copyArray = [1, 2, 3, 4, 5, 6];
97+
copyArray = copyArray.copyWithin(1, 3, 5); // pos 3-4 values are copied to pos 1-2
98+
console.log('copyArray.copyWithin(1, 3, 5)', copyArray);
99+
100+
//* ********* methods find and findIndex
101+
console.log('methods find and findIndex');
102+
103+
function multipleOf13(element) {
104+
return element % 13 === 0;
105+
}
106+
107+
console.log('numbers.find(multipleOf13)', numbers.find(multipleOf13));
108+
console.log('numbers.findIndex(multipleOf13)', numbers.findIndex(multipleOf13));
109+
110+
//* ********* EcmaScript 2016 (ES7): using the method includes
111+
console.log('EcmaScript 2016 (ES7): using the method includes');
112+
113+
console.log('numbers.includes(15)', numbers.includes(15));
114+
console.log('numbers.includes(20)', numbers.includes(20));
115+
116+
const numbers5 = [7, 6, 5, 4, 3, 2, 1];
117+
console.log('numbers5.includes(4, 5)', numbers5.includes(4, 5));

‎examples/chapter02/06-ES6Methods.js

-114
This file was deleted.

‎examples/chapter02/06-ES6Methods.html ‎examples/chapter02/07-Sorting.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
<title></title>
66
</head>
77
<body>
8-
<script type="text/javascript" src="06-ES6Methods.js"></script>
8+
<script type="text/javascript" src="07-Sorting.js"></script>
99
</body>
10-
</html>
10+
</html>

‎examples/chapter02/07-Sorting.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// @ts-check
2+
3+
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
4+
console.log('numbers', numbers);
5+
6+
7+
numbers = numbers.reverse();
8+
console.log('numbers.reverse()', numbers);
9+
10+
console.log('numbers.sort()', numbers.sort());
11+
12+
/* console.log(
13+
numbers.sort(function(a, b) {
14+
return a - b;
15+
})
16+
); */ // ES5 syntax
17+
18+
console.log('numbers.sort((a, b) => a - b)', numbers.sort((a, b) => a - b));
19+
20+
function compare(a, b) {
21+
if (a < b) {
22+
return -1;
23+
}
24+
if (a > b) {
25+
return 1;
26+
}
27+
// a must be equal to b
28+
return 0;
29+
}
30+
31+
console.log('numbers.sort(compare)', numbers.sort(compare));
32+
33+
// Sorting objects
34+
const friends = [
35+
{ name: 'John', age: 30 },
36+
{ name: 'Ana', age: 20 },
37+
{ name: 'Chris', age: 25 }, // trailing comma ES2017
38+
];
39+
40+
function comparePerson(a, b) {
41+
if (a.age < b.age) {
42+
return -1;
43+
}
44+
if (a.age > b.age) {
45+
return 1;
46+
}
47+
return 0;
48+
}
49+
50+
console.log('friends.sort(comparePerson)', friends.sort(comparePerson));
51+
52+
let names = ['Ana', 'ana', 'john', 'John'];
53+
console.log('names', names);
54+
console.log('names.sort()', names.sort());
55+
56+
names = ['Ana', 'ana', 'john', 'John']; // reset to array original state
57+
console.log('names.sort(compareFunction)', names.sort((a, b) => {
58+
if (a.toLowerCase() < b.toLowerCase()) {
59+
return -1;
60+
}
61+
if (a.toLowerCase() > b.toLowerCase()) {
62+
return 1;
63+
}
64+
return 0;
65+
}));
66+
names = ['Ana', 'ana', 'john', 'John']; // reset to array original state
67+
console.log('names.sort((a, b) => a.localeCompare(b))', names.sort((a, b) => a.localeCompare(b)));
68+
69+
const names2 = ['Maève', 'Maeve'];
70+
console.log('names2', names);
71+
console.log('names2.sort((a, b) => a.localeCompare(b))', names2.sort((a, b) => a.localeCompare(b)));

‎examples/chapter02/08-Searching.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="08-Searching.js"></script>
9+
</body>
10+
</html>

‎examples/chapter02/08-Searching.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @ts-check
2+
3+
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
4+
console.log('numbers', numbers);
5+
6+
//* ** toString
7+
console.log('numbers.toString()', numbers.toString());
8+
9+
console.log('numbers.indexOf(10)', numbers.indexOf(10));
10+
console.log('numbers.indexOf(100)', numbers.indexOf(100));
11+
12+
numbers.push(10);
13+
console.log('push 10: numbers.lastIndexOf(10)', numbers.lastIndexOf(10));
14+
console.log('push 10: numbers.lastIndexOf(100)', numbers.lastIndexOf(100));
15+
16+
const numbersString = numbers.join('-');
17+
console.log('numbers.join("-")', numbersString);

‎examples/chapter02/07-TypedArrays.html ‎examples/chapter02/09-TypedArrays.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
<title></title>
66
</head>
77
<body>
8-
<script type="text/javascript" src="07-TypedArrays.js"></script>
8+
<script type="text/javascript" src="09-TypedArrays.js"></script>
99
</body>
10-
</html>
10+
</html>

‎examples/chapter02/07-TypedArrays.js ‎examples/chapter02/09-TypedArrays.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
let length = 5;
2-
let int16 = new Int16Array(length);
1+
const length = 5;
2+
const int16 = new Int16Array(length);
33

4-
let array16 = [];
4+
const array16 = [];
55
array16.length = length;
66

77
for (let i = 0; i < length; i++) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// const numbers: number[];
2+
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
3+
console.log('numbers', numbers);
4+
5+
// let names: string[];
6+
let names = ['Ana', 'ana', 'john', 'John'];
7+
console.log('names', names);
8+
console.log('names.sort()', names.sort());
9+
10+
interface Person {
11+
name: string;
12+
age: number;
13+
}
14+
15+
// const friends: {name: string, age: number}[];
16+
const friends = [
17+
{ name: 'John', age: 30 },
18+
{ name: 'Ana', age: 20 },
19+
{ name: 'Chris', age: 25 }, // trailing comma ES2017
20+
];
21+
22+
function comparePerson(a: Person, b: Person) {
23+
if (a.age < b.age) {
24+
return -1;
25+
}
26+
if (a.age > b.age) {
27+
return 1;
28+
}
29+
return 0;
30+
}
31+
32+
console.log('friends.sort(comparePerson)', friends.sort(comparePerson));

‎examples/index.html

+23-4
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
<div class="mdl-layout__tab-bar mdl-js-ripple-effect">
3838
<a href="#scroll-tab-1" class="mdl-layout__tab is-active">01</a>
3939
<a href="#scroll-tab-2" class="mdl-layout__tab">02</a>
40-
<a href="#scroll-tab-2" class="mdl-layout__tab">03</a>
41-
<a href="#scroll-tab-2" class="mdl-layout__tab">04</a>
42-
<a href="#scroll-tab-2" class="mdl-layout__tab">05</a>
43-
<a href="#scroll-tab-2" class="mdl-layout__tab">06</a>
40+
<a href="#scroll-tab-3" class="mdl-layout__tab">03</a>
41+
<a href="#scroll-tab-3" class="mdl-layout__tab">04</a>
42+
<a href="#scroll-tab-3" class="mdl-layout__tab">05</a>
43+
<a href="#scroll-tab-3" class="mdl-layout__tab">06</a>
4444
</div>
4545
</header>
4646
<main class="mdl-layout__content">
@@ -75,6 +75,25 @@
7575
</div>
7676
</section>
7777
<section class="mdl-layout__tab-panel" id="scroll-tab-2">
78+
<div class="page-content">
79+
<div class="page-content mdl-layout--fixed-drawer">
80+
<div class="mdl-layout__drawer is-visible">
81+
<nav class="mdl-navigation">
82+
<a class="mdl-navigation__link" href="chapter02/01-Introduction.html">01-Introduction</a>
83+
<a class="mdl-navigation__link" href="chapter02/02-CreatingAndInitialingArrays.html" >02-Creating Initialing Arrays</a>
84+
<a class="mdl-navigation__link" href="chapter02/03-AddingRemovingElements.html" >03-Adding Removing Elements</a>
85+
<a class="mdl-navigation__link" href="chapter02/04-TwoDimensionalMultiDimensional.html" >04-Two Dimensional - MultiDimensional</a>
86+
<a class="mdl-navigation__link" href="chapter02/05-ArrayMethods.html" >05-ArrayMethods</a>
87+
<a class="mdl-navigation__link" href="chapter02/06-ES6Methods.html" >06-ES2015+ Methods</a>
88+
<a class="mdl-navigation__link" href="chapter02/07-Sorting.html" >07-Sorting</a>
89+
<a class="mdl-navigation__link" href="chapter02/08-Searching.html" >08-Searching</a>
90+
<a class="mdl-navigation__link" href="chapter02/09-TypedArrays.html" >09-TypedArrays</a>
91+
</nav>
92+
</div>
93+
</div>
94+
</div>
95+
</section>
96+
<section class="mdl-layout__tab-panel" id="scroll-tab-3">
7897
<div class="page-content">
7998
Soon.
8099
</div>

0 commit comments

Comments
 (0)
Please sign in to comment.