-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
90 lines (73 loc) · 2.86 KB
/
script.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
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
document.addEventListener("DOMContentLoaded", function() {
const loader = document.querySelector(".custom-loader");
const content = document.getElementById("content");
// Simulating delay for demonstration purposes
setTimeout(() => {
loader.style.display = "none";
content.style.display = "block";
}, 3000);
const exitButton = document.getElementById("exitButton");
exitButton.addEventListener("click", closeApplication);
function closeApplication() {
window.close();
}
const convertButton = document.getElementById("convertButton");
const clearButton = document.getElementById("clearButton");
const inputValue = document.getElementById("inputValue");
const resultValue = document.getElementById("resultValue");
inputValue.setAttribute("autocomplete", "off"); // Disable autocomplete
convertButton.addEventListener("click", convertTemperature);
clearButton.addEventListener("click", clearFields);
inputValue.addEventListener("input", validateInput);
function convertTemperature() {
const value = inputValue.value;
const unitFrom = document.getElementById("unitFrom").value;
const unitTo = document.getElementById("unitTo").value;
if (!isValidNumber(value)) {
alert("Please enter a valid number");
return;
}
const result = convert(value, unitFrom, unitTo);
resultValue.value = result;
}
function clearFields() {
inputValue.value = "";
document.getElementById("unitFrom").selectedIndex = 0;
document.getElementById("unitTo").selectedIndex = 0;
resultValue.value = "";
}
function validateInput() {
const value = inputValue.value;
if (!isValidNumber(value)) {
alert("Please enter a valid number");
}
}
function isValidNumber(value) {
return /^-?\d*\.?\d*$/.test(value);
}
function convert(value, fromUnit, toUnit) {
if (fromUnit === toUnit) {
return value + " " + toUnit;
}
if (fromUnit === "celsius") {
if (toUnit === "fahrenheit") {
return (value * 9 / 5) + 32 + " °F";
} else if (toUnit === "kelvin") {
return parseFloat(value) + 273.15 + " K";
}
} else if (fromUnit === "fahrenheit") {
if (toUnit === "celsius") {
return (value - 32) * 5 / 9 + " °C";
} else if (toUnit === "kelvin") {
return (value - 32) * 5 / 9 + 273.15 + " K";
}
} else if (fromUnit === "kelvin") {
if (toUnit === "celsius") {
return value - 273.15 + " °C";
} else if (toUnit === "fahrenheit") {
return (value - 273.15) * 9 / 5 + 32 + " °F";
}
}
return "Invalid conversion";
}
});