Answer to Question #48141 in AJAX | JavaScript | HTML | PHP for tom

Question #48141
Hi Im having a project with a temperature converter and this was the html code provided to me:

<!DOCTYPE html>
<html>

<head>
<title>Temperature Converter</title>
<script type="text/javascript" src="temp_converter"></script>
</head>

<body>
<p>Enter a Temperature to Convert in the field below and select the starting scale.</p>
<input type="text" name="txtTemp" id="txtTemp" placeholder="Enter a Temperature" />
<br />
<br />
<input type="radio" id="radFahrenheit" name="radTemp" checked/>Fahrenheit to Celsius




<input type="radio" id="radCelcius" name="radTemp"/>Celsius to Fahrenheit








<button name="btnConvert" id="btnConvert">Convert</button>





<div id="divResults"></div>
</body>

</html>

and heres this:

var temp;
var outputArea;

function ConvertTemp(){
temp = parseInt(document.getElementById('txtTemp').value);
outputArea = document.getElementById('divResults');
var radF = document.getElementById('radFahrenheit');
var radC = document.getElementById('radCelsius');

if(radF.checked){
//Call Fahrenheit to Celsius
FtoC(temp);
}else if(radC.checked){
//Call Celsius to Fahrenheit
CtoF(temp);
}
}

//Fahrenheit to Celsius
function FtoC(temp){
C = (temp - 32) * (5 / 9);
outputArea.innerHTML = temp + " in Fahrenheit is " + C + " in Celsius.";
}

//Celsius to Fahrenheit
function CtoF(temp){
F = (temp * (9 / 5)) + 32;
outputArea.innerHTML = temp + " in Celsius is " + F + " in Fahrenheit.";
}
1
Expert's answer
2014-10-28T01:44:28-0400
Correct HTMLcode:

<!DOCTYPEhtml>
<html>

<head>
<title>TemperatureConverter</title>
<scripttype="text/javascript"
src="temp_converter.js"></script>
</head>

<body>
<p>Entera Temperature to Convert in the field below and select the starting
scale.</p>
<inputtype="text" name="txtTemp" id="txtTemp"
placeholder="Enter a Temperature" />
<br />
<br />
<inputtype="radio" id="radFahrenheit" name="radTemp"
checked/>Fahrenheit to Celsius
<br/>
<br/>
<inputtype="radio" id="radCelsius"
name="radTemp"/>Celsius to Fahrenheit
<br/>
<br/>
<br/>
<br/>
<buttonname="btnConvert" id="btnConvert"
onClick="ConvertTemp();">Convert</button>
<br/>
<br/>

<divid="divResults"></div>
</body>

</html>

temp_converter.js (not changed)

var temp;
var outputArea;

function ConvertTemp(){
temp =parseInt(document.getElementById('txtTemp').value);
outputArea = document.getElementById('divResults');
var radF = document.getElementById('radFahrenheit');
var radC = document.getElementById('radCelsius');

if(radF.checked){
//Call Fahrenheit to Celsius
FtoC(temp);
}else if(radC.checked){
//Call Celsius to Fahrenheit
CtoF(temp);
}
}

//Fahrenheit to Celsius
function FtoC(temp){
C = (temp - 32) * (5 / 9);
outputArea.innerHTML = temp + " in Fahrenheit is" + C + " in Celsius.";
}

//Celsius to Fahrenheit
function CtoF(temp){
F = (temp * (9 / 5)) + 32;
outputArea.innerHTML = temp + " in Celsius is" + F + " in Fahrenheit.";
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS