-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (36 loc) · 1.4 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
let celciusInput = document.querySelector('#celcius > input')
let fahrenheitInput = document.querySelector('#fahrenheit> input')
let kelvinInput = document.querySelector('#kelvin > input')
let btn = document.querySelector('.button button')
function roundNumber(number) {
return Math.round(number * 100) / 100
}
/* Celcius to Fahrenheit and Kelvin */
celciusInput.addEventListener('input', function () {
let cTemp = parseFloat(celciusInput.value)
let fTemp = (cTemp * (9 / 5)) + 32
let kTemp = cTemp + 273.15
fahrenheitInput.value = roundNumber(fTemp)
kelvinInput.value = roundNumber(kTemp)
})
/* Fahrenheit to Celcius and Kelvin */
fahrenheitInput.addEventListener('input', function () {
let fTemp = parseFloat(fahrenheitInput.value)
let cTemp = (fTemp - 32) * (5 / 9)
let kTemp = (fTemp - 32) * (5 / 9) + 273.15
celciusInput.value = roundNumber(cTemp)
kelvinInput.value = roundNumber(kTemp)
})
/* Kelvin to Celcius and Fahrenheit */
kelvinInput.addEventListener('input', function () {
let kTemp = parseFloat(kelvinInput.value)
let cTemp = kTemp - 273.15
let fTemp = (kTemp - 273.15) * (9 / 5) + 32
celciusInput.value = roundNumber(cTemp)
fahrenheitInput.value = roundNumber(fTemp)
})
btn.addEventListener('click', () => {
celciusInput.value = ""
fahrenheitInput.value = ""
kelvinInput.value = ""
})