forked from exercism/DEPRECATED.javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
120 lines (100 loc) · 2.7 KB
/
example.js
File metadata and controls
120 lines (100 loc) · 2.7 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
(function() {
'use strict';
var CustomSet = function(inputData) {
this.data = inputData || [];
};
CustomSet.prototype.delete = function(element) {
var index = this.data.indexOf(element);
if (index !== -1) {
this.data.splice(index, 1);
}
return this;
};
CustomSet.prototype.difference = function(other) {
var thisData = this.data.sort();
var thatData = other.data.sort();
var result = [];
for (var i=0; i < thisData.length; i++) {
if (thatData.indexOf(thisData[i]) === -1) {
result.push(thisData[i]);
}
}
return new CustomSet(result);
};
CustomSet.prototype.disjoint = function(other) {
if (this.data.length === 0) { return false };
for (var i = 0; i < this.data.length; i++) {
if (other.data.indexOf(this.data[i]) !== -1) {
return false;
}
}
return true;
};
CustomSet.prototype.empty = function() {
return new CustomSet([]);
};
CustomSet.prototype.intersection = function(other) {
var thisData = this.data.sort();
var thatData = other.data.sort();
var result = [];
for (var i=0; i < thisData.length; i++) {
if (thatData.indexOf(thisData[i]) !== -1) {
result.push(thisData[i]);
}
}
return new CustomSet(result);
};
CustomSet.prototype.member = function(datum) {
return this.data.indexOf(datum) !== -1;
};
CustomSet.prototype.put = function(datum) {
if (this.data.indexOf(datum) === -1) {
this.data.push(datum);
}
return this;
};
CustomSet.prototype.size = function() {
return arrayUnique(this.data).length;
};
CustomSet.prototype.subset = function(other) {
for (var i=0; i < other.data.length; i++) {
if (this.data.indexOf(other.data[i]) === -1) {
return false;
}
}
return true;
};
CustomSet.prototype.toList = function() {
return arrayUnique(this.data);
};
CustomSet.prototype.union = function(other) {
var result = [];
for (var i=0; i < this.data.length; i++) {
result.push(this.data[i]);
}
for (var j=0; j < other.data.length; j++) {
result.push(other.data[j]);
}
return new CustomSet(arrayUnique(result));
};
CustomSet.prototype.eql = function(other) {
var thisData = this.data.sort();
var thatData = other.data.sort();
if (!other || this.data.length !== other.data.length) {
return false;
}
for (var i = 0; i < this.length; i++) {
if (thisData[i] !== otherData[i]) {
return false;
}
}
return true;
};
var arrayUnique = function(a) {
return a.reduce(function(p, c) {
if (p.indexOf(c) < 0) p.push(c);
return p;
}, []);
};
module.exports = CustomSet;
})();