/
2_40.js
59 lines (48 loc) · 1.27 KB
/
2_40.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
class Gear {
constructor(chainring, cog, wheel = null) {
this._chainring = chainring;
this._cog = cog;
this._wheel = wheel;
}
get chainring() { return this._chainring; }
get cog() { return this._cog; }
get wheel() { return this._wheel; }
ratio() {
return this.chainring / this.cog;
}
gear_inches() {
return this.ratio() * this.wheel.diameter();
}
}
class Wheel {
constructor(rim, tire) {
this._rim = rim;
this._tire = tire;
}
get rim() { return this._rim; }
get tire() { return this._tire; }
diameter() {
return this.rim + (this.tire * 2);
}
circumference() {
return this.diameter() * Math.PI;
}
}
const wheel = new Wheel(26, 1.5);
console.log(wheel.circumference());
// => 91.106186954104
console.log(new Gear(52, 11, wheel).gear_inches());
// => 137.0909090909091
console.log(new Gear(52, 11).ratio());
// => 4.7272727272727275
/*
* EXTENSION: Note that calling `gear_inches` without supplying a wheel results
* in an error as gear_inches attempts to send the `diameter` message to a
* `null` value.
*
* A similar error would occur in Ruby, with an undefined method `diameter` on
* the `nil:NilClass`.
*/
console.log(new Gear(52, 11).gear_inches());
// => TypeError: Cannot read property 'diameter' of null
// ...