/
2_30.js
87 lines (70 loc) · 1.89 KB
/
2_30.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
class RevealingReferences {
constructor(data) {
this._wheels = this.wheelify(data);
}
get wheels() {
return this._wheels;
}
// first - iterate over the array
diameters() {
return this.wheels.map((wheel) => this.diameter(wheel));
}
// second - calculate diameter of ONE wheel
diameter(wheel) {
return wheel.rim + (wheel.tire * 2);
}
wheelify(data) {
return data.map((cell) => {
return {
rim: cell[0],
tire: cell[1],
};
});
}
}
const data = [[622, 20], [622, 23], [559, 30], [559, 40]];
console.log(`RevealingReferences diameters = ${new RevealingReferences(data).diameters()}`);
// gear_inches does too much
class Gear {
constructor(chainring, cog, rim, tire) {
this._chainring = chainring;
this._cog = cog;
this._rim = rim;
this._tire = tire;
}
get chainring() { return this._chainring; }
get cog() { return this._cog; }
get rim() { return this._rim; }
get tire() { return this._tire; }
ratio() {
return this.chainring / this.cog;
}
gear_inches() {
// tire goes around rim twice for diameter
return this.ratio() * (this.rim + (this.tire * 2));
}
}
console.log(`1st Gear gear_inches = ${new Gear(54, 11, 622, 20).gear_inches()}`);
// diameter method doesn't belong in Gear, notice it depends on rim and tire only
class GearWithDiameter {
constructor(chainring, cog, rim, tire) {
this._chainring = chainring;
this._cog = cog;
this._rim = rim;
this._tire = tire;
}
get chainring() { return this._chainring; }
get cog() { return this._cog; }
get rim() { return this._rim; }
get tire() { return this._tire; }
ratio() {
return this.chainring / this.cog;
}
gear_inches() {
return this.ratio() * this.diameter();
}
diameter() {
return this.rim + (this.tire * 2);
}
}
console.log(`2nd Gear gear_inches = ${new Gear(54, 11, 622, 20).gear_inches()}`);