generated from rstropek/git-fundamentals-pull-request
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
44 lines (38 loc) · 1.68 KB
/
index.js
File metadata and controls
44 lines (38 loc) · 1.68 KB
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
$(() => {
const valueInput = $('#valueInput');
const fromScaleUnit = $('#fromScaleUnit');
const toScaleUnit = $('#toScaleUnit');
const convertButton = $('#convert');
const resultText = $('#result');
// ============================================================================================
// Add conversions here
const conversion = [
// You can reference functions
{ from: 'm', to: 'cm', convertFunc: fromMeterToCentimeter },
// You can specify inline conversions
{ from: 'cm', to: 'm', convertFunc: value => value / 100 },
{ from: 'C', to: 'F', convertFunc: value => value * 9 / 5 + 32 },
{ from: 'F', to: 'C', convertFunc: value => (value - 32) * 5 / 9 },
];
function fromMeterToCentimeter(value) {
return value * 100;
}
// ============================================================================================
conversion.forEach(c => fromScaleUnit.append($('<option>', { text: c.from })));
fromScaleUnit.val(conversion[0].from);
refreshToSelect();
fromScaleUnit.change(refreshToSelect);
convertButton.click(() => {
if (valueInput.val() && fromScaleUnit.val() && toScaleUnit.val()) {
const result = conversion
.find(c => c.from === fromScaleUnit.val() && c.to == toScaleUnit.val())
.convertFunc(valueInput.val());
resultText.text(`The result is ${result}`);
}
});
function refreshToSelect() {
toScaleUnit.find('option').remove();
conversion.filter(c => c.from === fromScaleUnit.val())
.forEach(c => toScaleUnit.append($('<option>', { text: c.to })));
}
});