-
Notifications
You must be signed in to change notification settings - Fork 0
/
sets.js
98 lines (85 loc) · 1.93 KB
/
sets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//Sets
function mySet(){
var collection = [];
this.has = function(value){
if(collection.indexOf(value)!== -1){
return true
}
return false
}
this.values = function(){
return collection;
}
this.add = function(value){
if(this.has(value)){
return false
}
collection.push(value);
return true
}
this.delete = function(value){
if(this.has(value)){
index = collection.indexOf(value)
collection.splice(index,1);
return true
}
collection.pop(value);
return true
}
this.size = function(){
return collection.length()
}
this.union = function(another){
var anotherCollection = new mySet();
var firstSet = this.values();
var secondSet = another.values();
firstSet.forEach(function(e){
anotherCollection.add(e)
});
secondSet.forEach(function(e){
anotherCollection.add(e)
});
return anotherCollection
}
this.intersection = function(another){
var intersaction = new mySet();
var firstSet = this.values();
firstSet.forEach(function(e){
if(another.has(e)){
intersaction.add(e)
}
});
return intersaction
}
this.difference = function (another){
var difference = new mySet();
var firstSet = this.values();
firstSet.forEach(function(e){
if(!another.has(e)){
difference.add(e)
}
});
return difference
}
this.subset = function(otherSet) {
var firstSet = this.values();
return firstSet.every(function(value) {
return otherSet.has(value);
});
};
}
var seta = new mySet();
var setb = new mySet();
seta.add('1');
seta.add('2');
seta.add('3');
setb.add('1');
setb.add('2');
setb.add('4');
setb.add('5');
setb.add('3');
console.log(seta.subset(setb));
console.log(seta.intersection(setb).values())
console.log(seta.union(setb).values())
console.log(seta.values())
console.log(setb.values())