|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+const US_LIQUID_GALLONS_TO_CUBIC_INCHES = 231
|
|
|
2
|
+const CUBIC_INCHES_TO_US_LIQUID_GALLONS = 1 / US_LIQUID_GALLONS_TO_CUBIC_INCHES
|
|
|
3
|
+
|
|
|
4
|
+const toLiquid = cubicInches => cubicInches * CUBIC_INCHES_TO_US_LIQUID_GALLONS
|
|
|
5
|
+const fromLiquid = gallons => gallons * US_LIQUID_GALLONS_TO_CUBIC_INCHES
|
|
|
6
|
+
|
|
|
7
|
+class Container {
|
|
|
8
|
+ constructor({ l, w, h }, level = 0) {
|
|
|
9
|
+ this.l = l
|
|
|
10
|
+ this.w = w
|
|
|
11
|
+ this.h = h
|
|
|
12
|
+ this.level = level
|
|
|
13
|
+ }
|
|
|
14
|
+ get liquidVolumeCapacity() {
|
|
|
15
|
+ return toLiquid(this.l * this.w * this.h)
|
|
|
16
|
+ }
|
|
|
17
|
+ get liquidVolumeFilled() {
|
|
|
18
|
+ return toLiquid(this.l * this.w * this.level)
|
|
|
19
|
+ }
|
|
|
20
|
+ get liquidVolumeRemaining() {
|
|
|
21
|
+ return this.liquidVolumeCapacity - this.liquidVolumeFilled
|
|
|
22
|
+ }
|
|
|
23
|
+}
|
|
|
24
|
+
|
|
|
25
|
+/** Functional Core */
|
|
|
26
|
+const _changeLevel = (amount, container) => {
|
|
|
27
|
+ const alteredLevel =
|
|
|
28
|
+ fromLiquid(container.liquidVolumeFilled + amount) /
|
|
|
29
|
+ container.l /
|
|
|
30
|
+ container.w
|
|
|
31
|
+ return createContainer(container, alteredLevel)
|
|
|
32
|
+}
|
|
|
33
|
+const createContainer = ({ l, w, h }, level) => {
|
|
|
34
|
+ if (Math.sign(l) + Math.sign(w) + Math.sign(h) !== 3)
|
|
|
35
|
+ return console.error(
|
|
|
36
|
+ `[Error] All dimensions must be positive and greater than zero!`
|
|
|
37
|
+ )
|
|
|
38
|
+ return new Container({ l, w, h }, level)
|
|
|
39
|
+}
|
|
|
40
|
+const fill = ({ container, amount }) => _changeLevel(amount, container)
|
|
|
41
|
+const drain = ({ container, amount }) => _changeLevel(-1 * amount, container)
|
|
|
42
|
+
|
|
|
43
|
+myTank = createContainer({ l: 12, w: 12, h: 12 })
|
|
|
44
|
+console.log('capacity:', myTank.liquidVolumeCapacity)
|
|
|
45
|
+console.log('filled:', myTank.liquidVolumeFilled)
|
|
|
46
|
+
|
|
|
47
|
+myTank = fill({ amount: myTank.liquidVolumeCapacity, container: myTank })
|
|
|
48
|
+console.log('\nremaining liquid volume:', myTank.liquidVolumeRemaining)
|
|
|
49
|
+console.log('remain h:', myTank.h - myTank.level)
|
|
|
50
|
+
|
|
|
51
|
+myTank = drain({ amount: 4, container: myTank })
|
|
|
52
|
+console.log('\nremaining liquid volume:', myTank.liquidVolumeRemaining)
|
|
|
53
|
+console.log('remain h:', myTank.h - myTank.level)
|
|
|
54
|
+console.log('filled:', myTank.liquidVolumeFilled)
|
|
|
55
|
+
|
|
|
56
|
+myTank = fill({ amount: 3, container: myTank })
|
|
|
57
|
+console.log('\nremaining liquid volume:', myTank.liquidVolumeRemaining)
|
|
|
58
|
+console.log('remain h:', myTank.h - myTank.level)
|
|
|
59
|
+console.log('filled:', myTank.liquidVolumeFilled)
|