-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
40 lines (34 loc) · 1.21 KB
/
app.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
const celsiusField = document.querySelector("#celsius");
const degree = document.querySelector("#degree");
const convertBtn = document.querySelector("#convert-btn");
const tempType = document.querySelector("#temp-type");
window.addEventListener("load", () => {
degree.value = "";
celsiusField.innerHTML = "";
});
if(degree.value === ""){
convertBtn.setAttribute("disabled","");
setTimeout(() => {
convertBtn.removeAttribute('disabled');
}, 4000);
}
convertBtn.addEventListener("click", (e) => {
e.preventDefault();
convertToCelsius();
convertBtn.innerHTML = "<span class='icon'><i class='fa fa-spinner fa-spin'></i> Converting...</span>";
setTimeout(() => {
convertBtn.innerHTML ="<span>Convert</span>"
}, 1000);
});
function convertToCelsius() {
let inputValue = degree.value;
setTimeout( () => {
if (tempType.value === "fahrenheit") {
const FahrenheitToCelsius = (inputValue - 32) * (5 / 9);
celsiusField.innerHTML = `${FahrenheitToCelsius.toFixed(3)} °c`;
} else if (tempType.value === "kelvin") {
const KelvinToCelsius = inputValue - 273.15;
celsiusField.innerHTML = `${KelvinToCelsius.toFixed(3)} °c`;
}
}, 1200)
}