Skip to content

Commit

Permalink
Update script.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Neel-07 authored Oct 12, 2023
1 parent 061110e commit 7997505
Showing 1 changed file with 36 additions and 44 deletions.
80 changes: 36 additions & 44 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,41 @@
let celsiusInput = document.querySelector('#celsius > input')
let fahrenheitInput = document.querySelector('#fahrenheit > input')
let kelvinInput = document.querySelector('#kelvin > input')
let celsiusInput = document.querySelector('#celsiusInput');
let fahrenheitInput = document.querySelector('#fahrenheitInput');
let kelvinInput = document.querySelector('#kelvinInput');
let clearButton = document.querySelector('#clearButton');

let btn = document.querySelector('.button button')


function roundNumber(number){
return Math.round(number*100)/100
function roundNumber(number) {
return Math.round(number * 100) / 100;
}

function convertTemperatures() {
let cTemp = parseFloat(celsiusInput.value);
let fTemp = (cTemp * 9/5) + 32;
let kTemp = cTemp + 273.15;

/* 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)
})

fahrenheitInput.value = roundNumber(fTemp);
kelvinInput.value = roundNumber(kTemp);
}

btn.addEventListener('click', ()=>{
celsiusInput.value = ""
fahrenheitInput.value = ""
kelvinInput.value = ""
})
celsiusInput.addEventListener('input', convertTemperatures);
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);
});
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);
});

clearButton.addEventListener('click', function() {
celsiusInput.value = "";
fahrenheitInput.value = "";
kelvinInput.value = "";
});

0 comments on commit 7997505

Please sign in to comment.