Skip to content

Commit 7e2e647

Browse files
committed
merge second edition
2 parents 51eec82 + 10b24fd commit 7e2e647

File tree

107 files changed

+5426
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+5426
-0
lines changed

README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Learning JavaScript Data Structures and Algorithms
2+
====================================
3+
4+
Source code of **Learning JavaScript Data Structures and Algorithms** book.
5+
6+
| 1st edition | 2nd edition |
7+
| ------------- |:-------------:|
8+
| ![1st edition](https://d1ldz4te4covpm.cloudfront.net/sites/default/files/imagecache/ppv4_main_book_cover/4874OS_Learning%20JavaScript%20Data%20Structures%20and%20Algorithms.jpg) | ![2nd edition](https://d255esdrn735hr.cloudfront.net/sites/default/files/imagecache/ppv4_main_book_cover/5493OS_5348_Learning%20JavaScript%20Data%20Structures%20and%20Algorithms,%20Second%20Edition.jpg) |
9+
| [Book link](http://amzn.to/1Y1OWPx)| [Book link](http://amzn.to/1TSkcA1)|
10+
11+
Book link - first edition:
12+
- [Packt](https://www.packtpub.com/application-development/learning-javascript-data-structures-and-algorithms)
13+
- [Amazon](http://amzn.to/1Y1OWPx)
14+
- [Chinese version](http://www.ituring.com.cn/book/1613)
15+
- [Korean version](http://www.acornpub.co.kr/book/javascript-data-structure)
16+
17+
Book link - second edition:
18+
- [Packt](https://www.packtpub.com/web-development/learning-javascript-data-structures-and-algorithms-second-edition)
19+
- [Amazon](http://amzn.to/1TSkcA1)
20+
- [Brazilian Portuguese version](https://novatec.com.br/livros/estruturas-de-dados-algoritmos-em-javascript/)
21+
22+
### List of Chapters:
23+
24+
* 01: [JavaScript: a quick overview](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter01)
25+
* 02: [Arrays](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter02)
26+
* 03: [Stacks](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter03)
27+
* 04: [Queues](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter04)
28+
* 05: [Linked Lists](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter05)
29+
* 06: [Sets](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter06)
30+
* 07: [Dictionaries and Hashes](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter07)
31+
* 08: [Trees](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter08)
32+
* 09: [Graphs](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter09)
33+
* 10: [Sorting and searching algorithms](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter10)
34+
* 11: [Pattern of algorithms](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter11)
35+
* 12: [Algorithm Complexity](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter12)
36+
37+
### First Edition source code:
38+
39+
Please refer to [this link](https://github.com/loiane/javascript-datastructures-algorithms/tree/master)
40+
41+
### Found an issue or have a question?
42+
43+
Please create an [Issue](https://github.com/loiane/javascript-datastructures-algorithms/issues) or [Pull Request](https://github.com/loiane/javascript-datastructures-algorithms/pulls)

chapter01/02-Variables.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var num = 1; //{1}
2+
num = 3; //{2}
3+
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
9+
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);
16+
17+
//******* Variable Scope
18+
19+
var myVariable = 'global';
20+
myOtherVariable = 'global';
21+
22+
function myFunction(){
23+
var myVariable = 'local';
24+
return myVariable;
25+
}
26+
27+
function myOtherFunction(){
28+
myOtherVariable = 'local';
29+
return myOtherVariable;
30+
}
31+
32+
console.log(myVariable); //{1}
33+
console.log(myFunction()); //{2}
34+
35+
console.log(myOtherVariable); //{3}
36+
console.log(myOtherFunction()); //{4}
37+
console.log(myOtherVariable); //{5}

chapter01/08-Functions.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function sayHello() {
2+
console.log('Hello!');
3+
}
4+
5+
sayHello();
6+
7+
/* function with parameter */
8+
function output(text) {
9+
console.log(text);
10+
}
11+
12+
output('Hello!');
13+
14+
output('Hello!', 'Other text');
15+
16+
output();
17+
18+
/* function using the return statement */
19+
function sum(num1, num2) {
20+
return num1 + num2;
21+
}
22+
23+
var result = sum(1,2);
24+
output(result);
25+
26+
27+
28+
29+

chapter01/10-ObjectOrientedJS.html

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

chapter01/10-ObjectOrientedJS.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* Object example 1 */
2+
3+
var obj = new Object();
4+
5+
/* Object example 2 */
6+
7+
var obj = {};
8+
9+
obj = {
10+
name: {
11+
first: 'Gandalf',
12+
last: 'the Grey'
13+
},
14+
address: 'Middle Earth'
15+
};
16+
17+
/* 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+
}
26+
}
27+
28+
var book = new Book('title', 'pag', 'isbn');
29+
30+
console.log(book.title); //outputs the book title
31+
32+
book.title = 'new title'; //update the value of the book title
33+
34+
console.log(book.title); //outputs the updated value
35+
36+
Book.prototype.printTitle = function(){
37+
console.log(this.title);
38+
};
39+
40+
book.printTitle();
41+
42+
book.printIsbn();

chapter01/11-ES6letconst.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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="11-ES6letconst.js"></script>
9+
10+
</body>
11+
</html>

chapter01/11-ES6letconst.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//******* EcmaScript 6: let and const keywords
2+
// EcmaScript 6 Constants
3+
const PI = 3.141593;
4+
//PI = 3.0; //throws error
5+
console.log(PI);
6+
7+
//******* EcmaScript 6: let is the new var
8+
var framework = 'Angular';
9+
var framework = 'React';
10+
console.log(framework);
11+
12+
let language = 'JavaScript!';
13+
//let language = 'Ruby!'; //throws error
14+
console.log(language);
15+
16+
//******* EcmaScript 6: variables scope
17+
let movie = 'Lord of the Rings';
18+
//var movie = 'Batman v Superman'; //throws error, variable movie already declared
19+
20+
function starWarsFan(){
21+
let movie = 'Star Wars';
22+
return movie;
23+
}
24+
25+
function marvelFan(){
26+
movie = 'The Avengers';
27+
return movie;
28+
}
29+
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);
41+
}
42+
43+
console.log(movie);
44+
console.log(starWarsFan());
45+
console.log(marvelFan());
46+
console.log(movie);
47+
blizzardFan();

chapter01/12-Es6StringTemplates.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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="12-Es6StringTemplates.js"></script>
9+
10+
</body>
11+
</html>

chapter01/12-Es6StringTemplates.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Template literals
2+
var book = {
3+
name: 'Learning JavaScript DataStructures and Algorithms'
4+
};
5+
6+
console.log(`You are reading ${book.name}.,
7+
and this is a new line`);

chapter01/13-ES6ArrowFunctions.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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="13-ES6ArrowFunctions.js"></script>
9+
10+
</body>
11+
</html>

chapter01/13-ES6ArrowFunctions.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//ES6: arrow functions
2+
let circleArea = (r) => {
3+
const PI = 3.14;
4+
let area = PI * r * r;
5+
return area;
6+
}
7+
console.log(circleArea(2));
8+
9+
let circleArea2 = (r) => 3.14 * r * r;
10+
console.log(circleArea2(2));
+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="14-ES6ParameterHandling.js"></script>
9+
</body>
10+
</html>

chapter01/14-ES6ParameterHandling.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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
6+
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
18+
19+
//******* EcmaScript 6: spread operator ('...')
20+
var params = [3, 4, 5];
21+
console.log(sum(...params));
22+
23+
var numbers = [1, 2, ...params]; //pushing values into array
24+
console.log(numbers);
25+
26+
//******* EcmaScript 6: rest parameter ('...')
27+
function restParamaterFunction (x, y, ...a) {
28+
return (x + y) * a.length;
29+
}
30+
console.log(restParamaterFunction(1, 2, "hello", true, 7)); // outputs 9;
31+
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));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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="15-ES6EnhancedObjectProperties.js"></script>
9+
10+
</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Destructuring Assignment + Property Shorthand
2+
var [x, y] = ['a', 'b'];
3+
var obj = { x, y };
4+
console.log(obj); // { x: "a", y: "b" }
5+
6+
[x, y] = [y, x];
7+
var temp = x;
8+
x = y;
9+
y = temp;
10+
11+
//code above is the same as
12+
var x = 'a';
13+
var y = 'b';
14+
var obj2 = { x: x, y: y };
15+
console.log(obj2); // { x: "a", y: "b" }
16+
17+
18+
// Method Properties
19+
var hello = {
20+
name : 'abcdef',
21+
printHello(){
22+
console.log('Hello');
23+
}
24+
}
25+
console.log(hello.printHello());
26+
27+
//code above is the same as:
28+
var hello2 = {
29+
name: 'abcdef',
30+
printHello: function printHello() {
31+
console.log('Hello');
32+
}
33+
};
34+
console.log(hello2.printHello());

chapter01/16-ES6Classes.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="16-ES6Classes.js"></script>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)