-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (49 loc) · 1.98 KB
/
index.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
document.getElementById("submitButton").onclick = function () {
let temp = Number(document.getElementById("textBox").value);
const sourceUnitElement = document.querySelector('input[name="fromUnit"]:checked');
const targetUnitElement = document.querySelector('input[name="toUnit"]:checked');
if (!sourceUnitElement || !targetUnitElement) {
displayMessage("You need to choose the temperature units");
return;
}
const sourceUnit = sourceUnitElement.value;
const targetUnit = targetUnitElement.value;
const targetUnitSymbol = targetUnitElement.dataset.symbol;
// we use kelvin as the base temp, this removes the need to account for possible combination of selections
var converter = {
celsius: {
toKelvin: temp => temp + 273.15,
fromKelvin: temp => temp - 273.15
},
fahrenheit: {
toKelvin: temp => (temp - 32) * 5 / 9 + 273.15,
fromKelvin: temp => (temp - 273.15) * 9 / 5 + 32
},
rankine: {
toKelvin: temp => temp * 5 / 9,
fromKelvin: temp => temp * 1.8
},
romer: {
toKelvin: temp => (temp - 7.5) / 0.52500 + 273.15,
fromKelvin: temp => (temp - 273.15) * 21 / 40 + 7.5
},
reaumur: {
toKelvin: temp => temp * 4 / 5 + 273.15,
fromKelvin: temp => (temp - 273.15) * 0.8
},
newton: {
toKelvin: temp => temp * 100 / 33 + 273.15,
fromKelvin: temp => (temp - 273.15) * 0.33000
},
delisle: {
toKelvin: temp => 373.15 - (temp * 2 / 3),
fromKelvin: temp => (temp - 273.15) * 1.5000 - 100.00
},
};
if (sourceUnit != "kelvin") temp = converter[sourceUnit].toKelvin(temp);
if (targetUnit != "kelvin") temp = converter[targetUnit].fromKelvin(temp);
displayMessage(`${temp}°${targetUnitSymbol}`);
}
function displayMessage(string) {
document.getElementById("result").innerHTML = string;
}