Skip to content

Commit 2ccfeef

Browse files
committedSep 8, 2017
added chapter 01
1 parent de901ab commit 2ccfeef

20 files changed

+349
-287
lines changed
 

Diff for: ‎.eslintrc.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"max-params": 0,
4848
"max-statements": 0,
4949
"new-cap": [2, { "newIsCap": true, "capIsNew": false }],
50-
"newline-after-var": [2, "always"],
50+
"newline-after-var": [0, "always"],
5151
"new-parens": 2,
5252
"no-alert": 0,
5353
"no-array-constructor": 2,
@@ -152,7 +152,7 @@
152152
"semi-spacing": 0,
153153
"sort-vars": 0,
154154
"space-before-blocks": [2, "always"],
155-
"space-before-function-paren": [2, {"anonymous": "always", "named": "never"}],
155+
"space-before-function-paren": [0, {"anonymous": "always", "named": "never"}],
156156
"space-in-brackets": 0,
157157
"space-in-parens": [2, "never"],
158158
"space-infix-ops": 2,
@@ -162,7 +162,7 @@
162162
"use-isnan": 2,
163163
"valid-jsdoc": 0,
164164
"valid-typeof": 2,
165-
"vars-on-top": 2,
165+
"vars-on-top": 0,
166166
"wrap-iife": [2, "any"],
167167
"wrap-regex": 0,
168168
"yoda": [2, "never"]

Diff for: ‎.travis.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ node_js:
33
- node
44
install:
55
- npm install
6-
76
script:
87
- npm run go
9-
- npm run coverage
10-
118
after_script:
129
- "cat coverage/lcov.info | node_modules/coveralls/bin/coveralls.js"
10+
notifications:
11+
email:
12+
on_failure: change
13+
on_success: change

Diff for: ‎README.md

+50-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,56 @@ Learning JavaScript Data Structures and Algorithms
22
====================================
33

44
[![Build Status](https://travis-ci.org/loiane/javascript-datastructures-algorithms.svg?branch=third-edition)](https://travis-ci.org/loiane/javascript-datastructures-algorithms)
5-
65
[![codecov](https://codecov.io/gh/loiane/javascript-datastructures-algorithms/branch/third-edition/graph/badge.svg)](https://codecov.io/gh/loiane/javascript-datastructures-algorithms)
76

87
Source code of **Learning JavaScript Data Structures and Algorithms** book.
8+
9+
Work in Progress.
10+
11+
## List of available chapters:
12+
13+
* 01: [JavaScript, ECMAScript and TypeScript: a quick overview](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/html/chapter01)
14+
* 02: [Arrays](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/html/chapter02)
15+
16+
17+
## Installing and running the book examples With Node
18+
19+
* Install [Node](https://nodejs.org)
20+
* Open terminal/cmd and change directoty to this project folder: `cd /Users/.../javascript-datastructures-algorithms` (Linux/Max) or `cd C:/.../javascript-datastructures-algorithms`
21+
* run `npm install` to install all depencies
22+
* To see the examples, run `http-server html` or `npm run serve`. Open your browser `http:\\localhost:8080` to see the book examples
23+
* Or `cd html/chapter01` and run each javascript file with node: `node 02-Variables`
24+
25+
## Running the book examples in the browser
26+
27+
* Right click on the html file you would like to see the examples, right click and 'Open with Chrome (or any other browser)'
28+
29+
## List of all available examples:
30+
31+
* 01: JavaScript, ECMAScript and TypeScript: a quick overview
32+
- 01-HelloWorld
33+
- 02-Variables
34+
- 03-Operators
35+
- 04-TruthyFalsy
36+
- 05-EqualsOperators
37+
- 06-ConditionalStatements
38+
- 07-Loops
39+
- 08-Functions
40+
- 10-ObjectOrientedJS
41+
- 11-ES6letconst
42+
- 12-Es6StringTemplates
43+
- 13-ES6ArrowFunctions
44+
- 14-ES6ParameterHandling
45+
- 15-ES6EnhancedObjectProperties
46+
- 16-ES6Classes
47+
- 17-TypeScript
48+
* 02: Arrays
49+
- 01-Introduction
50+
- 02-CreatingAndInitialingArrays
51+
- 03-AddingRemovingElements
52+
- 04-TwoDimensionalMultiDimensional
53+
- 05-ArrayMethods
54+
- 06-ES6Methods
55+
- 07-TypedArrays
56+
57+
Happy Coding!

Diff for: ‎html/chapter01/01-HelloWorld.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
// @ts-check
2+
13
function output(t) {
2-
document.write('<p>' + t + '</p>');
3-
return;
4+
document.write('<p>' + t + '</p>');
5+
return;
46
}
57

68
alert('Hello, World!');
79
console.log('Hello, World!');
8-
output('Hello, World!');
10+
output('Hello, World!');

Diff for: ‎html/chapter01/02-Variables.js

+25-23
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
1-
var num = 1; //{1}
2-
num = 3; //{2}
1+
// @ts-check
32

4-
var price = 1.5; //{3}
5-
var name = 'Packt'; //{4}
6-
var trueValue = true; //{5}
7-
var nullVar = null; //{6}
8-
var und; //7
3+
var num = 1; // {1}
4+
num = 3; // {2}
95

10-
console.log("num: "+ num);
11-
console.log("name: "+ name);
12-
console.log("trueValue: "+ trueValue);
13-
console.log("price: "+ price);
14-
console.log("nullVar: "+ nullVar);
15-
console.log("und: "+ und);
6+
var price = 1.5; // {3}
7+
var name = 'Packt'; // {4}
8+
var trueValue = true; // {5}
9+
var nullVar = null; // {6}
10+
var und; // {7}
1611

17-
//******* Variable Scope
12+
console.log('num: ' + num);
13+
console.log('name: ' + name);
14+
console.log('trueValue: ' + trueValue);
15+
console.log('price: ' + price);
16+
console.log('nullVar: ' + nullVar);
17+
console.log('und: ' + und);
18+
19+
// ******* Variable Scope
1820

1921
var myVariable = 'global';
2022
myOtherVariable = 'global';
2123

22-
function myFunction(){
23-
var myVariable = 'local';
24-
return myVariable;
24+
function myFunction() {
25+
var myVariable = 'local';
26+
return myVariable;
2527
}
2628

27-
function myOtherFunction(){
28-
myOtherVariable = 'local';
29-
return myOtherVariable;
29+
function myOtherFunction() {
30+
myOtherVariable = 'local';
31+
return myOtherVariable;
3032
}
3133

32-
console.log(myVariable); //{1}
34+
console.log(myVariable); //{1}
3335
console.log(myFunction()); //{2}
3436

35-
console.log(myOtherVariable); //{3}
37+
console.log(myOtherVariable); //{3}
3638
console.log(myOtherFunction()); //{4}
37-
console.log(myOtherVariable); //{5}
39+
console.log(myOtherVariable); //{5}

Diff for: ‎html/chapter01/03-Operators.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @ts-check
2+
13
/* Arithmetic operators */
24
var num = 0;
35

@@ -21,8 +23,7 @@ num--;
2123

2224
console.log('New num value is ' + num);
2325

24-
console.log('num mod 2 value is ' + (num % 2));
25-
26+
console.log('num mod 2 value is ' + num % 2);
2627

2728
/* Assignment operators */
2829
num += 1;
@@ -45,24 +46,24 @@ console.log('num <= 1 : ' + (num <= 1));
4546
/* Logical operators */
4647
console.log('true && false : ' + (true && false));
4748
console.log('true || false : ' + (true || false));
48-
console.log('!true : ' + (!true));
49+
console.log('!true : ' + !true);
4950

5051
/* Bitwise operators */
51-
console.log('5 & 1:', (5 & 1)); //same as 0101 & 0001 (result 0001 / 1)
52-
console.log('5 | 1:', (5 | 1)); //same as 0101 | 0001 (result 0101 / 5)
53-
console.log('~ 5:', (~5)); //same as ~0101 (result 1010 / 10)
54-
console.log('5 ^ 1:', (5 ^ 1)); //same as 0101 ^ 0001 (result 0100 / 4)
55-
console.log('5 << 1:', (5 << 1)); //same as 0101 << 1 (result 1010 / 10)
56-
console.log('5 >> 1:', (5 >> 1)); //same as 0101 >> 1 (result 0010 / 2)
52+
console.log('5 & 1:', 5 & 1); // same as 0101 & 0001 (result 0001 / 1)
53+
console.log('5 | 1:', 5 | 1); // same as 0101 | 0001 (result 0101 / 5)
54+
console.log('~ 5:', ~5); // same as ~0101 (result 1010 / 10)
55+
console.log('5 ^ 1:', 5 ^ 1); // same as 0101 ^ 0001 (result 0100 / 4)
56+
console.log('5 << 1:', 5 << 1); // same as 0101 << 1 (result 1010 / 10)
57+
console.log('5 >> 1:', 5 >> 1); // same as 0101 >> 1 (result 0010 / 2)
5758

5859
/* typeOf */
5960
console.log('typeof num:', typeof num);
6061
console.log('typeof Packt:', typeof 'Packt');
6162
console.log('typeof true:', typeof true);
62-
console.log('typeof [1,2,3]:', typeof [1,2,3]);
63-
console.log('typeof {name:John}:', typeof {name:'John'});
63+
console.log('typeof [1,2,3]:', typeof [1, 2, 3]);
64+
console.log('typeof {name:John}:', typeof { name: 'John' });
6465

6566
/* delete */
66-
var myObj = {name: 'John', age: 21};
67+
var myObj = { name: 'John', age: 21 };
6768
delete myObj.age;
68-
console.log(myObj); //Object {name: "John"}
69+
console.log(myObj); // Object {name: "John"}

Diff for: ‎html/chapter01/04-TruthyFalsy.js

+19-17
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
function testTruthy(val){
2-
return val ? console.log('truthy') : console.log('falsy');
1+
// @ts-check
2+
3+
function testTruthy(val) {
4+
return val ? console.log('truthy') : console.log('falsy');
35
}
46

5-
testTruthy(true); //true
6-
testTruthy(false); //false
7-
testTruthy(new Boolean(false)); //true (object is always true)
7+
testTruthy(true); // true
8+
testTruthy(false); // false
9+
testTruthy(new Boolean(false)); // true (object is always true)
810

9-
testTruthy(''); //false
10-
testTruthy('Packt'); //true
11-
testTruthy(new String('')); //true (object is always true)
11+
testTruthy(''); // false
12+
testTruthy('Packt'); // true
13+
testTruthy(new String('')); // true (object is always true)
1214

13-
testTruthy(1); //true
14-
testTruthy(-1); //true
15-
testTruthy(NaN); //false
16-
testTruthy(new Number(NaN)); //true (object is always true)
15+
testTruthy(1); // true
16+
testTruthy(-1); // true
17+
testTruthy(NaN); // false
18+
testTruthy(new Number(NaN)); // true (object is always true)
1719

18-
testTruthy({}); //true (object is always true)
20+
testTruthy({}); // true (object is always true)
1921

20-
var obj = {name:'John'};
21-
testTruthy(obj); //true
22-
testTruthy(obj.name); //true
23-
testTruthy(obj.age); //age (prop does not exist)
22+
var obj = { name: 'John' };
23+
testTruthy(obj); // true
24+
testTruthy(obj.name); // true
25+
testTruthy(obj.age); // age (prop does not exist)

Diff for: ‎html/chapter01/05-EqualsOperators.js

+30-28
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
1-
//Packt == true
1+
// @ts-check
2+
3+
// Packt == true
24

35
console.log('packt' ? true : false);
4-
//outputs true
6+
// outputs true
57

68
console.log('packt' == true);
7-
//1 - converts Boolean using toNumber
8-
//'packt' == 1
9-
//2 - converts String using toNumber
10-
//NaN == 1
11-
//outputs false
9+
// 1 - converts Boolean using toNumber
10+
// 'packt' == 1
11+
// 2 - converts String using toNumber
12+
// NaN == 1
13+
// outputs false
1214

1315
console.log('packt' == false);
14-
//1 - converts Boolean using toNumber
15-
//'packt' == 0
16-
//2 - converts String using toNumber
17-
//NaN == 0
18-
//outputs false
16+
// 1 - converts Boolean using toNumber
17+
// 'packt' == 0
18+
// 2 - converts String using toNumber
19+
// NaN == 0
20+
// outputs false
1921

2022
console.log([0] == true);
21-
//1 - converts Boolean using toNumber
22-
//[0] == 1
23-
//2 - converts Object using toPrimitive
24-
//2.1 - [0].valueOf() is not primitive
25-
//2.2 - [0].toString is 0
26-
//0 == 1
27-
//outputs false
28-
29-
//******************************* ===
30-
console.log('packt' === true); //false
31-
32-
console.log('packt' === 'packt'); //true
33-
34-
var person1 = {name:'John'};
35-
var person2 = {name:'John'};
36-
console.log(person1 === person2); //false, different objects
23+
// 1 - converts Boolean using toNumber
24+
// [0] == 1
25+
// 2 - converts Object using toPrimitive
26+
// 2.1 - [0].valueOf() is not primitive
27+
// 2.2 - [0].toString is 0
28+
// 0 == 1
29+
// outputs false
30+
31+
//* ****************************** ===
32+
console.log('packt' === true); // false
33+
34+
console.log('packt' === 'packt'); // true
35+
36+
var person1 = { name: 'John' };
37+
var person2 = { name: 'John' };
38+
console.log(person1 === person2); // false, different objects

Diff for: ‎html/chapter01/06-ConditionalStatements.js

+28-26
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,53 @@
1+
// @ts-check
2+
13
/* Example 01 - if */
24
var num = 1;
35
if (num === 1) {
4-
console.log("num is equal to 1");
6+
console.log('num is equal to 1');
57
}
68

79
/* Example 02 - if-else */
810
var num = 0;
911
if (num === 1) {
10-
console.log("num is equal to 1");
12+
console.log('num is equal to 1');
1113
} else {
12-
console.log("num is not equal to 1, the value of num is " + num);
14+
console.log('num is not equal to 1, the value of num is ' + num);
1315
}
1416

1517
/* Example 03 - if-else-if-else... */
1618
var month = 5;
1719
if (month === 1) {
18-
console.log("January");
19-
} else if (month === 2){
20-
console.log("February");
21-
} else if (month === 3){
22-
console.log("March");
20+
console.log('January');
21+
} else if (month === 2) {
22+
console.log('February');
23+
} else if (month === 3) {
24+
console.log('March');
2325
} else {
24-
console.log("Month is not January, February or March");
26+
console.log('Month is not January, February or March');
2527
}
2628

2729
/* Example 04 - switch */
2830
var month = 5;
29-
switch(month) {
30-
case 1:
31-
console.log("January");
32-
break;
33-
case 2:
34-
console.log("February");
35-
break;
36-
case 3:
37-
console.log("March");
38-
break;
39-
default:
40-
console.log("Month is not January, February or March");
31+
switch (month) {
32+
case 1:
33+
console.log('January');
34+
break;
35+
case 2:
36+
console.log('February');
37+
break;
38+
case 3:
39+
console.log('March');
40+
break;
41+
default:
42+
console.log('Month is not January, February or March');
4143
}
4244

4345
/* Example 05 - ternary operator - if..else */
44-
if (num === 1){
45-
num--;
46+
if (num === 1) {
47+
num--;
4648
} else {
47-
num++;
49+
num++;
4850
}
4951

50-
//is the same as
51-
(num === 1) ? num-- : num++;
52+
// is the same as
53+
num === 1 ? num-- : num++;

Diff for: ‎html/chapter01/07-Loops.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
console.log('**** for example ****');
2-
32
/* for - example */
4-
for (var i=0; i<10; i++) {
5-
console.log(i);
3+
for (var i = 0; i < 10; i++) {
4+
console.log(i);
65
}
76

87
console.log('**** while example ****');
98
/* while - example */
109
var i = 0;
11-
while(i<10)
12-
{
13-
console.log(i);
14-
i++;
10+
while (i < 10) {
11+
console.log(i);
12+
i++;
1513
}
1614

1715
console.log('**** do-while example ****');
1816
/* do-while - example */
1917
var i = 0;
2018
do {
21-
console.log(i);
22-
i++;
23-
} while (i<10)
19+
console.log(i);
20+
i++;
21+
} while (i < 10);

Diff for: ‎html/chapter01/08-Functions.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
// @ts-check
2+
13
function sayHello() {
2-
console.log('Hello!');
4+
console.log('Hello!');
35
}
46

57
sayHello();
68

79
/* function with parameter */
810
function output(text) {
9-
console.log(text);
11+
console.log(text);
1012
}
1113

1214
output('Hello!');
@@ -17,13 +19,8 @@ output();
1719

1820
/* function using the return statement */
1921
function sum(num1, num2) {
20-
return num1 + num2;
22+
return num1 + num2;
2123
}
2224

23-
var result = sum(1,2);
25+
var result = sum(1, 2);
2426
output(result);
25-
26-
27-
28-
29-

Diff for: ‎html/chapter01/10-ObjectOrientedJS.js

+21-22
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,41 @@
1-
/* Object example 1 */
1+
// @ts-check
22

3+
/* Object example 1 */
34
var obj = new Object();
45

56
/* Object example 2 */
6-
77
var obj = {};
88

99
obj = {
10-
name: {
11-
first: 'Gandalf',
12-
last: 'the Grey'
13-
},
14-
address: 'Middle Earth'
10+
name: {
11+
first: 'Gandalf',
12+
last: 'the Grey'
13+
},
14+
address: 'Middle Earth'
1515
};
1616

1717
/* Object example 3 */
18-
19-
function Book(title, pages, isbn){
20-
this.title = title;
21-
this.pages = pages;
22-
this.isbn = isbn;
23-
this.printIsbn = function(){
24-
console.log(this.isbn);
25-
}
18+
function Book(title, pages, isbn) {
19+
this.title = title;
20+
this.pages = pages;
21+
this.isbn = isbn;
22+
this.printIsbn = function() {
23+
console.log(this.isbn);
24+
};
2625
}
2726

28-
var book = new Book('title', 'pag', 'isbn');
27+
var book = new Book('title', 'pag', 'isbn');
2928

30-
console.log(book.title); //outputs the book title
29+
console.log(book.title); // outputs the book title
3130

32-
book.title = 'new title'; //update the value of the book title
31+
book.title = 'new title'; // update the value of the book title
3332

34-
console.log(book.title); //outputs the updated value
33+
console.log(book.title); // outputs the updated value
3534

36-
Book.prototype.printTitle = function(){
37-
console.log(this.title);
35+
Book.prototype.printTitle = function() {
36+
console.log(this.title);
3837
};
3938

4039
book.printTitle();
4140

42-
book.printIsbn();
41+
book.printIsbn();

Diff for: ‎html/chapter01/11-ES6letconst.js

+26-24
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,49 @@
1-
//******* EcmaScript 6: let and const keywords
1+
// @ts-check
2+
3+
//* ****** EcmaScript 6: let and const keywords
24
// EcmaScript 6 Constants
35
const PI = 3.141593;
4-
//PI = 3.0; //throws error
6+
// PI = 3.0; //throws error
57
console.log(PI);
68

7-
//******* EcmaScript 6: let is the new var
9+
//* ****** EcmaScript 6: let is the new var
810
var framework = 'Angular';
911
var framework = 'React';
1012
console.log(framework);
1113

1214
let language = 'JavaScript!';
13-
//let language = 'Ruby!'; //throws error
15+
// let language = 'Ruby!'; //throws error
1416
console.log(language);
1517

16-
//******* EcmaScript 6: variables scope
18+
//* ****** EcmaScript 6: variables scope
1719
let movie = 'Lord of the Rings';
18-
//var movie = 'Batman v Superman'; //throws error, variable movie already declared
20+
// var movie = 'Batman v Superman'; //throws error, variable movie already declared
1921

20-
function starWarsFan(){
21-
let movie = 'Star Wars';
22-
return movie;
22+
function starWarsFan() {
23+
let movie = 'Star Wars';
24+
return movie;
2325
}
2426

25-
function marvelFan(){
26-
movie = 'The Avengers';
27-
return movie;
27+
function marvelFan() {
28+
movie = 'The Avengers';
29+
return movie;
2830
}
2931

30-
function blizzardFan(){
31-
let isFan = true;
32-
let phrase = 'Warcraft';
33-
console.log('Before if: ' + phrase);
34-
if (isFan){
35-
let phrase = 'initial text';
36-
phrase = 'For the Horde!';
37-
console.log('Inside if: ' + phrase);
38-
}
39-
phrase = 'For the Alliance!';
40-
console.log('After if: ' + phrase);
32+
function blizzardFan() {
33+
let isFan = true;
34+
let phrase = 'Warcraft';
35+
console.log('Before if: ' + phrase);
36+
if (isFan) {
37+
let phrase = 'initial text';
38+
phrase = 'For the Horde!';
39+
console.log('Inside if: ' + phrase);
40+
}
41+
phrase = 'For the Alliance!';
42+
console.log('After if: ' + phrase);
4143
}
4244

4345
console.log(movie);
4446
console.log(starWarsFan());
4547
console.log(marvelFan());
4648
console.log(movie);
47-
blizzardFan();
49+
blizzardFan();

Diff for: ‎html/chapter01/12-Es6StringTemplates.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
// @ts-check
2+
13
// Template literals
24
var book = {
3-
name: 'Learning JavaScript DataStructures and Algorithms'
5+
name: 'Learning JavaScript DataStructures and Algorithms'
46
};
57

68
console.log(`You are reading ${book.name}.,
7-
and this is a new line`);
9+
and this is a new line`);

Diff for: ‎html/chapter01/13-ES6ArrowFunctions.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
//ES6: arrow functions
2-
let circleArea = (r) => {
3-
const PI = 3.14;
4-
let area = PI * r * r;
5-
return area;
6-
}
1+
// @ts-check
2+
3+
// ES6: arrow functions
4+
let circleArea = r => {
5+
const PI = 3.14;
6+
let area = PI * r * r;
7+
return area;
8+
};
79
console.log(circleArea(2));
810

9-
let circleArea2 = (r) => 3.14 * r * r;
10-
console.log(circleArea2(2));
11+
let circleArea2 = r => 3.14 * r * r;
12+
console.log(circleArea2(2));

Diff for: ‎html/chapter01/14-ES6ParameterHandling.js

+27-28
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
1-
//******* EcmaScript 6: Default Parameter Values
2-
function sum (x = 1, y = 2, z = 3) {
3-
return x + y + z
4-
};
5-
console.log(sum(4,2)); //outpus 9
1+
// @ts-check
62

7-
//function above is the same as
8-
function sum2 (x, y, z) {
9-
if (x === undefined)
10-
x = 1;
11-
if (y === undefined)
12-
y = 2;
13-
if (z === undefined)
14-
z = 3;
15-
return x + y + z;
16-
};
17-
console.log(sum2(4,2)); //outpus 10
3+
//* ****** EcmaScript 6: Default Parameter Values
4+
function sum(x = 1, y = 2, z = 3) {
5+
return x + y + z;
6+
}
7+
console.log(sum(4, 2)); // outpus 9
8+
9+
// function above is the same as
10+
function sum2(x, y, z) {
11+
if (x === undefined) x = 1;
12+
if (y === undefined) y = 2;
13+
if (z === undefined) z = 3;
14+
return x + y + z;
15+
}
16+
console.log(sum2(4, 2)); // outpus 10
1817

19-
//******* EcmaScript 6: spread operator ('...')
18+
//* ****** EcmaScript 6: spread operator ('...')
2019
var params = [3, 4, 5];
2120
console.log(sum(...params));
2221

23-
var numbers = [1, 2, ...params]; //pushing values into array
22+
var numbers = [1, 2, ...params]; // pushing values into array
2423
console.log(numbers);
2524

26-
//******* EcmaScript 6: rest parameter ('...')
27-
function restParamaterFunction (x, y, ...a) {
28-
return (x + y) * a.length;
25+
//* ****** EcmaScript 6: rest parameter ('...')
26+
function restParamaterFunction(x, y, ...a) {
27+
return (x + y) * a.length;
2928
}
30-
console.log(restParamaterFunction(1, 2, "hello", true, 7)); // outputs 9;
29+
console.log(restParamaterFunction(1, 2, 'hello', true, 7)); // outputs 9;
3130

32-
//code above is the same as ES5:
33-
function restParamaterFunction2 (x, y) {
34-
var a = Array.prototype.slice.call(arguments, 2);
35-
return (x + y) * a.length;
36-
};
37-
console.log(restParamaterFunction2(1, 2, "hello", true, 7));
31+
// code above is the same as ES5:
32+
function restParamaterFunction2(x, y) {
33+
var a = Array.prototype.slice.call(arguments, 2);
34+
return (x + y) * a.length;
35+
}
36+
console.log(restParamaterFunction2(1, 2, 'hello', true, 7));

Diff for: ‎html/chapter01/15-ES6EnhancedObjectProperties.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @ts-check
2+
13
// Destructuring Assignment + Property Shorthand
24
var [x, y] = ['a', 'b'];
35
var obj = { x, y };
@@ -8,27 +10,26 @@ var temp = x;
810
x = y;
911
y = temp;
1012

11-
//code above is the same as
13+
// code above is the same as
1214
var x = 'a';
1315
var y = 'b';
1416
var obj2 = { x: x, y: y };
1517
console.log(obj2); // { x: "a", y: "b" }
1618

17-
1819
// Method Properties
1920
var hello = {
20-
name : 'abcdef',
21-
printHello(){
22-
console.log('Hello');
23-
}
24-
}
21+
name: 'abcdef',
22+
printHello() {
23+
console.log('Hello');
24+
}
25+
};
2526
console.log(hello.printHello());
2627

27-
//code above is the same as:
28+
// code above is the same as:
2829
var hello2 = {
29-
name: 'abcdef',
30-
printHello: function printHello() {
31-
console.log('Hello');
32-
}
30+
name: 'abcdef',
31+
printHello: function printHello() {
32+
console.log('Hello');
33+
}
3334
};
34-
console.log(hello2.printHello());
35+
console.log(hello2.printHello());

Diff for: ‎html/chapter01/16-ES6Classes.js

+50-49
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,60 @@
1+
// @ts-check
2+
13
// ES6 classes
24
class Book {
3-
constructor (title, pages, isbn) {
4-
this.title = title;
5-
this.pages = pages;
6-
this.isbn = isbn;
7-
}
8-
printIsbn(){
9-
console.log(this.isbn);
10-
}
5+
constructor(title, pages, isbn) {
6+
this.title = title;
7+
this.pages = pages;
8+
this.isbn = isbn;
9+
}
10+
printIsbn() {
11+
console.log(this.isbn);
12+
}
1113
}
1214

13-
let book = new Book('title', 'pag', 'isbn');
14-
15-
console.log(book.title); //outputs the book title
15+
let book = new Book('title', 'pag', 'isbn');
1616

17-
book.title = 'new title'; //update the value of the book title
17+
console.log(book.title); // outputs the book title
1818

19-
console.log(book.title); //outputs the book title
19+
book.title = 'new title'; // update the value of the book title
2020

21+
console.log(book.title); // outputs the book title
2122

22-
//inheritance
23+
// inheritance
2324
class ITBook extends Book {
24-
25-
constructor (title, pages, isbn, technology) {
26-
super(title, pages, isbn);
27-
this.technology = technology;
28-
}
29-
30-
printTechnology(){
31-
console.log(this.technology);
32-
}
25+
constructor(title, pages, isbn, technology) {
26+
super(title, pages, isbn);
27+
this.technology = technology;
28+
}
29+
30+
printTechnology() {
31+
console.log(this.technology);
32+
}
3333
}
3434

35-
let jsBook = new ITBook('Learning JS Algorithms', '200', '1234567890', 'JavaScript');
35+
let jsBook = new ITBook(
36+
'Learning JS Algorithms',
37+
'200',
38+
'1234567890',
39+
'JavaScript'
40+
);
3641

3742
console.log(jsBook.title);
3843
console.log(jsBook.printTechnology());
3944

40-
//getter and setters
45+
// getter and setters
4146
class Person {
47+
constructor(name) {
48+
this._name = name;
49+
}
4250

43-
constructor (name) {
44-
this._name = name;
45-
}
51+
get name() {
52+
return this._name;
53+
}
4654

47-
get name() {
48-
return this._name;
49-
}
50-
51-
set name(value) {
52-
this._name = value;
53-
}
55+
set name(value) {
56+
this._name = value;
57+
}
5458
}
5559

5660
let lotrChar = new Person('Frodo');
@@ -61,28 +65,25 @@ console.log(lotrChar.name);
6165
lotrChar._name = 'Sam';
6266
console.log(lotrChar.name);
6367

64-
65-
//using symbols for private atributes
66-
68+
// using symbols for private atributes
6769
var _name = Symbol();
6870
class Person2 {
71+
constructor(name) {
72+
this[_name] = name;
73+
}
6974

70-
constructor (name) {
71-
this[_name] = name;
72-
}
73-
74-
get name() {
75-
return this[_name];
76-
}
75+
get name() {
76+
return this[_name];
77+
}
7778

78-
set name(value) {
79-
this[_name] = value;
80-
}
79+
set name(value) {
80+
this[_name] = value;
81+
}
8182
}
8283

8384
let lotrChar2 = new Person2('Frodo');
8485
console.log(lotrChar2.name);
8586
lotrChar2.name = 'Gandalf';
8687
console.log(lotrChar2.name);
8788

88-
console.log(Object.getOwnPropertySymbols(lotrChar2));
89+
console.log(Object.getOwnPropertySymbols(lotrChar2));

Diff for: ‎jsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
"strict": true
88
},
99
"exclude": ["node_modules", "dist"],
10-
"include": ["lib/js/**/*"]
10+
"include": ["lib/js/**/*","html/**"]
1111
}

Diff for: ‎package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@
1515
"homepage": "https://github.com/loiane/javascript-datastructures-algorithms",
1616
"scripts": {
1717
"clean": "rm -rf ./dist; mkdir ./dist",
18-
"lint": "node_modules/.bin/eslint src/index.js",
19-
"lint-test": "node_modules/.bin/eslint test/index.js",
2018
"build:js": "babel lib/js --presets babel-preset-es2015 --out-dir dist/js",
2119
"build:ts": "tsc",
2220
"build": "npm run build:js && npm run build:ts",
2321
"test:js": "mocha --compilers js:babel-core/register --colors -R spec --recursive ./test/js",
2422
"test:ts": "mocha -r ts-node/register --colors --recursive ./test/ts/**/*.spec.ts",
2523
"test": "npm run test:js && npm run test:ts",
2624
"coverage": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -R spec --recursive ./test/js -- --compilers js:babel-core/register && codecov",
27-
"go": "npm run clean && npm run test && npm run build",
28-
"serve": "http-serve html"
25+
"serve": "http-server html",
26+
"go": "npm run clean && npm run test && npm run build && npm run coverage"
2927
},
3028
"devDependencies": {
3129
"@types/chai": "^4.0.4",
@@ -38,9 +36,11 @@
3836
"chai": "^4.1.2",
3937
"codecov": "^2.3.0",
4038
"eslint": "^4.6.1",
39+
"http-server": "^0.10.0",
4140
"istanbul": "^v1.1.0-alpha.1",
4241
"mocha": "^3.5.0",
4342
"ts-node": "^3.3.0",
43+
"tslint": "^5.7.0",
4444
"typescript": "^2.5.2"
4545
}
4646
}

0 commit comments

Comments
 (0)
Please sign in to comment.