-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.js
More file actions
98 lines (76 loc) · 2.41 KB
/
Copy pathclasses.js
File metadata and controls
98 lines (76 loc) · 2.41 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
/* reviewing from objects.js */
class Shirt {
constructor (color, size) {
this.color = color;
this.size = size;
}
wear() {
return `You're wearing a size ${this.size} shirt that is ${this.color}.`;
}
}
let smallRedShirt = new Shirt('red', 'small');
console.log(smallRedShirt.wear()); // => You've consumed a small, red bean.
/* SUBCLASSES */
class dressShirt extends Shirt {
constructor (color, size, collarType) {
super(color, size); // <= passes in params from Shirt class
this.collarType = collarType;
}
}
let mediumBlueMandarinShirt = new dressShirt("blue", "medium", "mandarin");
console.log(mediumBlueMandarinShirt); // => dressShirt { color: 'blue', size: 'medium', collarType: 'mandarin' }
console.log(mediumBlueMandarinShirt.wear()); // => You're wearing a size medium shirt that is blue.
/* Build a data structure called Group to implement Set-like characteristics:
- add, delete, has methods
- add item only if it is not already inside group
- delete item only if it is in the group
- has returns boolean to indicate membership
- should also have 'from' static method that takes iterable and creates group
containing (one of each of) its values
*/
class Group {
constructor() {
this.values = [];
}
add(value) {
if (!this.values.includes(value)) {
this.values.push(value);
}
}
delete(value) {
if (this.values.includes(value)) {
this.values.splice(this.values.indexOf(value), 1);
}
}
has(value) {
if (this.values.includes(value)) {
return true;
} else {
return false;
}
}
static from(iterable) {
let newGroup = new Group;
for (let val of iterable) {
newGroup.add(val);
}
return newGroup;
}
}
/* create empty Group type object */
let someGroup = new Group();
/* add 5 to Group type object named someGroup */
someGroup.add(5);
console.log(someGroup);
/* add 1 */
someGroup.add(1);
console.log(someGroup);
/* delete 5 */
someGroup.delete(5);
console.log(someGroup);
/* does someGroup contain the value 1? */
console.log(someGroup.has(1));
/* create a Group type object from the array object [1,2,3,3,3,3] */
let particularGroup = Group.from([1,2,3,3,3,3]);
particularGroup.add(4); // add four to Group object named particularGroup
console.log(particularGroup); // => 1,2,3,4