-
Notifications
You must be signed in to change notification settings - Fork 0
/
inheritance.js
138 lines (109 loc) · 3.27 KB
/
inheritance.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Base class
function BaseBuilder() {
}
BaseBuilder.prototype.plus = function() {
for (var i = 0; i < arguments.length; i++) {
this.value += arguments[i];
}
return this;
};
BaseBuilder.prototype.minus = function() {};
BaseBuilder.prototype.multiply = function() {};
BaseBuilder.prototype.divide = function() {};
BaseBuilder.prototype.mod = function() {};
BaseBuilder.prototype.remove = function() {};
BaseBuilder.prototype.sub = function() {};
BaseBuilder.prototype.get = function() {
return this;
};
// ES6 IntBuilder class
class IntBuilder extends BaseBuilder {
constructor(value = 0) {
super();
this.value = value;
}
minus(...args) {
this.value -= args.reduce((acc, cur) => acc + cur, 0);
return this;
}
multiply(n) {
this.value *= n;
return this;
}
divide(n) {
this.value = Math.floor(this.value / n);
return this;
}
mod(n) {
this.value %= n;
return this;
}
static random(from, to) {
return Math.floor(Math.random() * (to - from + 1)) + from;
}
}
// ES5 StringBuilder class
function StringBuilder(value) {
if (typeof value !== 'undefined') {
this.value = value;
} else {this.value = ''}
}
StringBuilder.prototype = Object.create(BaseBuilder.prototype);
StringBuilder.prototype.constructor = StringBuilder;
StringBuilder.prototype.minus = function(n) {
this.value = this.value.slice(0, -n);
return this;
};
StringBuilder.prototype.multiply = function(n) {
var tempValue = this.value;
for (var i = 1; i < n; i++) {
this.value += tempValue;
}
return this;
};
StringBuilder.prototype.divide = function(n) {
var k = Math.floor(this.value.length / n);
this.value = this.value.slice(0, k);
return this;
};
StringBuilder.prototype.remove = function(str) {
while (this.value.includes(str)) {
var index = this.value.indexOf(str);
var length = str.length;
this.value = `${this.value.slice(0, index)}${this.value.slice(index + length)}`;
}
return this;
};
StringBuilder.prototype.sub = function(from, n) {
this.value = this.value.substr(from,n);
return this;
};
/*
//======================================EXAMPLES FOR TESTING THE CODE:=================================================
//TESTING IntBuilder
console.log(IntBuilder.random(10, 100)); // Random number;
let intBuilder = new IntBuilder(10); // 10;
console.log(
intBuilder
.plus(2, 3, 2) // 17;
.minus(1, 2) // 14;
.multiply(2) // 28;
.divide(4) // 7;
.mod(3) // 1;
.get() // -> 1;
)
//Testing StringBuilder
// EXAMPLE:
let strBuilder = new StringBuilder('Hello');
console.log(strBuilder);
console.log( // 'Hello';
strBuilder
.plus(' all', '!') // 'Hello all!'
.minus(4) // 'Hello '
.multiply(3) // 'Hello Hello Hello '
.divide(4) // 'Hell';
.remove('l') // 'He';
.sub(1,1) // 'e';
.get()
);
*/