Temperature conversion using JavaScript
In this, we can convert the temperature from celsius to fahrenheit or fahrenheit to celsius.
Code is given below :
1.HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2? family=Merriweather:ital,wght@1,300&display=swap" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container" id="container">
<h1>Temperature Conversion</h1>
<div class="int">
<label>Select Type to Convert : </label>
<select id="temp">
<option>Celsius</option>
<option>Fahrenheit</option>
</select>
</div>
<br>
<div class="int">
<label>Enter value : </label>
<input type="text" id="val">
</div><br>
<button onclick="convert()">SUBMIT</button>
<p id="result"></p>
</div>
</body>
</html>
2.CSS
body
{
margin: 0;
padding: 0;
font-family: sans-serif;
width: 50%;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
position: absolute;
text-align: center;
}
.container
{
border: 2px solid black;
}
h1
{
text-align: center;
color:#5DADE2;
font-family: 'Merriweather', serif;
margin-bottom: 20px;
}
select
{
font-size: 17px;
}
input
{
border: none;
outline: none;
border-bottom: 2px solid black;
font-size: 17px;
}
.int
{
padding: 5px;
font-size: 20px;
}
button
{
background: #EB984E;
border: none;
padding: 4px 25px 4px 25px;
border-radius: 20px;
font-size: 20px;
}
button:hover
{
color: #fff;
background: #E67E22;
}
p
{
font-size: 20px;
color: #111;
}
3.JavaScript
let type_conv=document.getElementById("temp");
let val=document.getElementById("val");
let result=0
function convert()
{
if(isNaN(val.value))
{
alert('Values should be numbers.')
window.location.reload()
}
else if(val.value=='')
{
alert('It must be filled out.')
window.location.reload();
}
else if(val.value==0)
{
alert('0 cannot be converted.')
window.location.reload()
}
else
{
if(type_conv.value==="Celsius")
{
result=(val.value-32)/1.8;
document.getElementById("result").innerHTML="Celsius : "+result+"'C";
}
else
{
result=(val.value*1.8)+32;
document.getElementById("result").innerHTML="Fahrenheit : "+result+"'F";
}
document.getElementById("container").style.borderColor="#EB984E";
}
}
OUTPUT :
Fig. Conversion of Celsius to Fahrenheit. |
Fig. Conversion of Fahrenheit to Celsius. Thank You |
Comments
Post a Comment