Skip to content

Commit 2ccfeef

Browse files
committed
added chapter 01
1 parent de901ab commit 2ccfeef

20 files changed

+349
-287
lines changed

.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"]

.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

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!

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!');

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}

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"}

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)

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

0 commit comments

Comments
 (0)