Skip to content

Commit 0a445cc

Browse files
committed
added chapter 06
1 parent 4e95061 commit 0a445cc

File tree

5 files changed

+238
-0
lines changed

5 files changed

+238
-0
lines changed

chapter06/01-Set.js

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/**
2+
* ECMSCRIPT 6 already have a Set class implementation:
3+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
4+
* We will try to copy the same functionalities
5+
* @constructor
6+
*/
7+
function Set() {
8+
9+
var items = {};
10+
11+
this.add = function(value){
12+
if (!this.has(value)){
13+
items[value] = value;
14+
return true;
15+
}
16+
return false;
17+
};
18+
19+
this.remove = function(value){
20+
if (this.has(value)){
21+
delete items[value];
22+
return true;
23+
}
24+
return false;
25+
};
26+
27+
this.has = function(value){
28+
return value in items;
29+
};
30+
31+
this.clear = function(){
32+
items = {};
33+
};
34+
35+
/**
36+
* Modern browsers function
37+
* IE9+, FF4+, Chrome5+, Opera12+, Safari5+
38+
* @returns {Number}
39+
*/
40+
this.size = function(){
41+
return Object.keys(items).length;
42+
};
43+
44+
/**
45+
* cross browser compatibility - legacy browsers
46+
* for modern browsers use size function
47+
* @returns {number}
48+
*/
49+
this.sizeLegacy = function(){
50+
var count = 0;
51+
for(var prop in items) {
52+
if(items.hasOwnProperty(prop))
53+
++count;
54+
}
55+
return count;
56+
};
57+
58+
/**
59+
* Modern browsers function
60+
* IE9+, FF4+, Chrome5+, Opera12+, Safari5+
61+
* @returns {Array}
62+
*/
63+
this.values = function(){
64+
return Object.keys(items);
65+
};
66+
67+
this.valuesLegacy = function(){
68+
var keys = [];
69+
for(var key in items){
70+
keys.push(key);
71+
}
72+
return keys;
73+
};
74+
75+
this.getItems = function(){
76+
return items;
77+
};
78+
79+
this.union = function(otherSet){
80+
var unionSet = new Set(); //{1}
81+
82+
var values = this.values(); //{2}
83+
for (var i=0; i<values.length; i++){
84+
unionSet.add(values[i]);
85+
}
86+
87+
values = otherSet.values(); //{3}
88+
for (var i=0; i<values.length; i++){
89+
unionSet.add(values[i]);
90+
}
91+
92+
return unionSet;
93+
};
94+
95+
this.intersection = function(otherSet){
96+
var insertectionSet = new Set(); //{1}
97+
98+
var values = this.values();
99+
for (var i=0; i<values.length; i++){ //{2}
100+
if (otherSet.has(values[i])){ //{3}
101+
insertectionSet.add(values[i]); //{4}
102+
}
103+
}
104+
105+
return insertectionSet;
106+
};
107+
108+
this.difference = function(otherSet){
109+
var differenceSet = new Set(); //{1}
110+
111+
var values = this.values();
112+
for (var i=0; i<values.length; i++){ //{2}
113+
if (!otherSet.has(values[i])){ //{3}
114+
differenceSet.add(values[i]); //{4}
115+
}
116+
}
117+
118+
return differenceSet;
119+
};
120+
121+
this.subset = function(otherSet){
122+
123+
if (this.size() > otherSet.size()){ //{1}
124+
return false;
125+
} else {
126+
var values = this.values();
127+
for (var i=0; i<values.length; i++){ //{2}
128+
if (!otherSet.has(values[i])){ //{3}
129+
return false; //{4}
130+
}
131+
}
132+
return true;
133+
}
134+
};
135+
}

chapter06/02-UsingSets.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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="01-Set.js"></script>
9+
<script type="text/javascript" src="02-UsingSets.js"></script>
10+
</body>
11+
</html>

chapter06/02-UsingSets.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var set = new Set();
2+
3+
set.add(1);
4+
console.log(set.values()); //outputs ["1"]
5+
console.log(set.has(1)); //outputs true
6+
console.log(set.size()); //outputs 1
7+
8+
set.add(2);
9+
console.log(set.values()); //outputs ["1", "2"]
10+
console.log(set.has(2)); //true
11+
console.log(set.size()); //2
12+
13+
set.remove(1);
14+
console.log(set.values()); //outputs ["2"]
15+
16+
set.remove(2);
17+
console.log(set.values()); //outputs []

chapter06/03-Operations.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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="01-Set.js"></script>
9+
<script type="text/javascript" src="03-Operations.js"></script>
10+
</body>
11+
</html>

chapter06/03-Operations.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//--------- Union ----------
2+
3+
var setA = new Set();
4+
setA.add(1);
5+
setA.add(2);
6+
setA.add(3);
7+
8+
var setB = new Set();
9+
setB.add(4);
10+
setB.add(5);
11+
setB.add(6);
12+
13+
var unionAB = setA.union(setB);
14+
console.log(unionAB.values());
15+
16+
17+
//--------- Intersection ----------
18+
19+
var setA = new Set();
20+
setA.add(1);
21+
setA.add(2);
22+
setA.add(3);
23+
24+
var setB = new Set();
25+
setB.add(2);
26+
setB.add(3);
27+
setB.add(4);
28+
29+
var intersectionAB = setA.intersection(setB);
30+
console.log(intersectionAB.values());
31+
32+
//--------- Difference ----------
33+
34+
var setA = new Set();
35+
setA.add(1);
36+
setA.add(2);
37+
setA.add(3);
38+
39+
var setB = new Set();
40+
setB.add(2);
41+
setB.add(3);
42+
setB.add(4);
43+
44+
var differenceAB = setA.difference(setB);
45+
console.log(differenceAB.values());
46+
47+
//--------- Subset ----------
48+
49+
var setA = new Set();
50+
setA.add(1);
51+
setA.add(2);
52+
53+
var setB = new Set();
54+
setB.add(1);
55+
setB.add(2);
56+
setB.add(3);
57+
58+
var setC = new Set();
59+
setC.add(2);
60+
setC.add(3);
61+
setC.add(4);
62+
63+
console.log(setA.subset(setB));
64+
console.log(setA.subset(setC));

0 commit comments

Comments
 (0)