diff --git a/app.js b/app.js new file mode 100644 index 000000000..c6ad18676 --- /dev/null +++ b/app.js @@ -0,0 +1,45 @@ +let celsiusInput = document.querySelector("#celsius > 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 */ +celsiusInput.addEventListener("input", function () { + let cTemp = parseFloat(celsiusInput.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; + + celsiusInput.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; + + celsiusInput.value = roundNumber(cTemp); + fahrenheitInput.value = roundNumber(fTemp); +}); + +btn.addEventListener("click", () => { + celsiusInput.value = ""; + fahrenheitInput.value = ""; + kelvinInput.value = ""; +}); diff --git a/index.html b/index.html index 4ad4ffff6..ce6a0125f 100644 --- a/index.html +++ b/index.html @@ -1,12 +1,43 @@
- - - -