-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
244 lines (209 loc) · 6.22 KB
/
index.html
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<!DOCTYPE html>
<html>
<head>
<title>Alculator</title>
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link rel="apple-touch-icon" href="meta/apple-touch-icon.png">
<link rel="shortcut icon" sizes="196x196" href="meta/android-shortcut-icon.png">
<meta name="viewport" content="width=device-width, height=device-height, user-scalable=no, minimal-ui">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<style type="text/css">
body {
font-family: Helvetica, Arial, sans-serif;
text-align: center;
vertical-align: middle;
font-size: 32px;
background-color: #ABCDEF;
}
header {
margin: -20px 0 -40px 0;
}
.num-input {
border: 1px solid #ABCDEF;
font-size: 14px;
padding: 7px;
background: rgba(255,255,255,0.5);
margin: 5px 0 0 0;
width: 72px;
text-align: right;
border-radius: 4px;
}
.unit-selector {
-webkit-appearance: none;
border: 1px solid #ABCDEF;
font-size: 12px;
height: 30px;
width: 60px;
background: rgba(255,255,255,0.33);
border-radius: 4px;
padding: 7px;
-moz-appearance: none;
text-indent: 0.01px;
text-overflow: '';
}
.result-num {
font-weight: bold;
}
#alculate-btn {
background-color: #03C03C;
font-size: 18px;
border: 1px solid transparent;
height: 36px;
width: 100px;
text-align: center;
border-radius: 4px;
margin: 10px 0 15px 0;
}
#results {
font-size: 24px;
}
</style>
</head>
<body>
<header>
<h2>Alculator</h2>
</header>
<!-- <form> -->
<input class="num-input" id="volume" placeholder="volume">
<select class="unit-selector" id="volume-units">
<option value="oz">oz</option>
<option value="ml">ml</option>
<option value="l">L</option>
</select>
<br>
<input class="num-input" id="potency" placeholder="potency">
<select class="unit-selector" id="potency-units">
<option value="abv">% ABV</option>
<option value="proof">Proof</option>
</select>
<br>
<input class="num-input" id="price" placeholder="price">
<select class="unit-selector" id="price-units">
<option value="dollar">$</option>
<option value="pound">£</option>
<option value="euro">€</option>
<option value="yen">¥</option>
<!-- <option value="cent">¢</option> -->
<option value="snow">☃</option>
</select>
<!-- </form> -->
<br>
<button type="button" class="num-input" id="alculate-btn" onclick="alculate()">
Alculate!
</button>
<div id="results" style="display:none">
<div id="result-per-div">
<span class="result-num" id="result-per-value"></span>
<span id="result-units"> cc ethanol per </span>
<span id="result-currency"></span>
</div>
<span class="result-num" id="result-total"></span> cc total
</div>
<script type="text/javascript">
function alculate() {
var volume = validateAndParseVolume(document.getElementById("volume").value);
var potencyUnits = document.getElementById("potency-units").value;
var potency = validateAndParsePotency(
document.getElementById("potency").value, potencyUnits);
var price = validateAndParsePrice(document.getElementById("price").value);
var errors = getErrors(volume, potency, price);
if (errors !== "") {
document.getElementById("results").style.display = "display:none";
alert(errors);
return;
}
var volumeUnits = document.getElementById("volume-units").value;
var volumeCCs = volumeInCCs(volume.val, volumeUnits);
var decimalPotency = (potencyUnits === "abv" ? potency.val : potency.val / 2.0) / 100;
var priceSelect = document.getElementById("price-units");
var priceUnits = priceSelect.options[priceSelect.selectedIndex].text;
var totalCCs = volumeCCs * decimalPotency;
document.getElementById("result-per-value").innerHTML = price.val == 0 ?
"∞" : (volumeCCs * decimalPotency / price.val).toFixed(2);
document.getElementById("result-currency").innerHTML = priceUnits;
document.getElementById("result-total").innerHTML = totalCCs.toFixed(2);
document.getElementById("results").style.display = "";
}
function getErrors() {
var errors = [];
for (var i = 0; i < arguments.length; i++) {
var err = arguments[i].err;
if(err !== "") {
errors.push(err);
}
}
return errors.join("\n");
}
function validateAndParseVolume(volumeString) {
if (volumeString === "") {
return errorResult("Volume required");
}
var volume = parseMultiplicationString(volumeString);
if (isNaN(volume)) {
return errorResult("Unparsable volume");
} else if (volume < 0) {
return errorResult("Volume must be nonnegative");
}
return validResult(volume);
}
function validateAndParsePotency(potencyString, potencyUnits) {
if (potencyString === "") {
return errorResult("Potency required")
}
var potency = parseFloat(potencyString);
if (isNaN(potency)) {
return errorResult("Unparsable potency");
} else if (potencyUnits === "abv" && (potency < 0 || potency > 100)) {
return errorResult("% ABV must be in 0-100");
} else if (potency < 0 || potency > 200) {
return errorResult("Proof must be in 0-200");
}
return validResult(potency);
}
function validateAndParsePrice(priceString) {
if (priceString === "") {
return validResult(0);
}
var price = parseMultiplicationString(priceString);
if (isNaN(price)) {
return errorResult("Unparsable price");
} else if (price < 0) {
return errorResult("Price must be nonnegative");
}
return validResult(price);
}
function parseMultiplicationString(multString) {
var product = 1;
var parts = multString.split("*");
for (var i = 0; i < parts.length; i++) {
factor = parseFloat(parts[i]);
if (isNaN(factor)) {
return NaN;
}
product *= factor;
}
return product;
}
function validResult(value) {
return {val: value, err: ""};
}
function errorResult(errorString) {
return {val: 0, err: errorString};
}
function volumeInCCs(volume, volumeUnits) {
switch(volumeUnits) {
case "oz":
return volume * 29.574
case "l":
return volume * 1000;
case "ml":
return volume;
default:
// error
return NaN;
}
}
</script>
</body>
</html>