
Baljeet S. answered 10/31/14
Tutor
New to Wyzant
Professional Programmer/PHP/Mysql/HTML/Oracle Service Cloud
Hi John,
How are you doing? I just saw your post regarding JavaScript code for temperature conversion. I saw your code and it seems to be incorrect. Now I have made changes and it is working fine with accurate result. I have tested this code on my system and it is working perfectly.
It seems I cannot upload my updated file on this post. There is no option for file upload while answering your question. So I am trying just to copy the whole file content so I can help you. Please copy my code and make a new HTML file and save it. Here it goes
<!DOCTYPE html>
<html>
<head>
<meta name="author" content="Baljeet Singh">
<title>Temperature Converter</title>
<style>
#divResults{ font-weight:bold;font-size:80px;color:#4c4c4c;text-align:center;}
#formAlign{text-align:center;}
</style>
<script type="text/javascript" >
function ConvertTemp(){
var txtTemp = document.getElementById('txtTemp').value;
var readioTemp = document.getElementById('radTemp');
var formRadio = document.tempForm.radTemp;
var fahrenheitToCelcius = '';
var celciusToFahrenheit = '';
var i ;
if(txtTemp == ''){
alert("Please Enter Temprature");
}
for (i = 0; i < formRadio.length; i++) {
if (formRadio[i].checked) {
if(formRadio[i].value == 'FC'){ //Convert Temperature from Fahrenheit To Celcius
celciusToFahrenheit = (txtTemp - 32) * (5 / 9);
document.getElementById('divResults').innerHTML = txtTemp+ "ºF = " +celciusToFahrenheit+ "ºC";
}
if(formRadio[i].value == 'CF'){ //Convert Temperature from Celcius To Fahrenheit
fahrenheitToCelcius = (txtTemp * (9/5)) + 32;
document.getElementById('divResults').innerHTML = txtTemp+ "ºC = " +fahrenheitToCelcius+ "ºF";
}
}
}
}
</script>
</head>
<body id="formAlign">
<p>Enter a Temperature to Convert in the field below and select the starting scale.</p>
<form action="" method="post" name="tempForm">
<input type="text" name="txtTemp" id="txtTemp" value="" placeholder="Enter a Temperature" />
<br />
<br />
<input type="radio" name="radTemp" value="FC"/>Fahrenheit to Celcius
<br/>
<br/>
<input type="radio" name="radTemp" value="CF"/>Celcius to Fahrenheit
<br/>
<br/></form>
<br/>
<br/>
<button onclick="javascript:ConvertTemp();">Convert</button>
<br/>
<br/>
<div id="divResults"></div>
</body>
</html>
<html>
<head>
<meta name="author" content="Baljeet Singh">
<title>Temperature Converter</title>
<style>
#divResults{ font-weight:bold;font-size:80px;color:#4c4c4c;text-align:center;}
#formAlign{text-align:center;}
</style>
<script type="text/javascript" >
function ConvertTemp(){
var txtTemp = document.getElementById('txtTemp').value;
var readioTemp = document.getElementById('radTemp');
var formRadio = document.tempForm.radTemp;
var fahrenheitToCelcius = '';
var celciusToFahrenheit = '';
var i ;
if(txtTemp == ''){
alert("Please Enter Temprature");
}
for (i = 0; i < formRadio.length; i++) {
if (formRadio[i].checked) {
if(formRadio[i].value == 'FC'){ //Convert Temperature from Fahrenheit To Celcius
celciusToFahrenheit = (txtTemp - 32) * (5 / 9);
document.getElementById('divResults').innerHTML = txtTemp+ "ºF = " +celciusToFahrenheit+ "ºC";
}
if(formRadio[i].value == 'CF'){ //Convert Temperature from Celcius To Fahrenheit
fahrenheitToCelcius = (txtTemp * (9/5)) + 32;
document.getElementById('divResults').innerHTML = txtTemp+ "ºC = " +fahrenheitToCelcius+ "ºF";
}
}
}
}
</script>
</head>
<body id="formAlign">
<p>Enter a Temperature to Convert in the field below and select the starting scale.</p>
<form action="" method="post" name="tempForm">
<input type="text" name="txtTemp" id="txtTemp" value="" placeholder="Enter a Temperature" />
<br />
<br />
<input type="radio" name="radTemp" value="FC"/>Fahrenheit to Celcius
<br/>
<br/>
<input type="radio" name="radTemp" value="CF"/>Celcius to Fahrenheit
<br/>
<br/></form>
<br/>
<br/>
<button onclick="javascript:ConvertTemp();">Convert</button>
<br/>
<br/>
<div id="divResults"></div>
</body>
</html>
Please execute this code at your system. Please let me know if you still facing problem while executing.
Enjoy !!
Thanks !!
Baljeet Singh