I wrote the code. But I'm having tough time making it work. Can you help me?
<html>
<!-- convert.html KG -->
<!-- This page converts temperatures from Fahrenheit to Celsius, and Celsius to Fahrenheit -->
<!--------------------------------------...
<head>
<title>Temperature Conversion Page</title>
<script type="text/javascript">
function FahrToCelsius(tempInFahr)
// Assumes: tempInFahr is a temperature in Fahrenheit
// Assumes: tempInCelsius is a temperature in Celsius
// Returns: the equivalent temperature in Celsius
// Returns: the equivalent temperature in Fahrenheit
{
return (5/9) * (tempInFahr - 32);
return (9/5) * (tempInCels + 32);
}
function Convert(FtoC)
// Assumes: document.TempForm.fahrBox contains degrees Fahrenheit
// Results: assigns document.TempForm.celsiusBox the equiv. temperature
{
var tempF, tempC; tempF = parseFloat(document.TempForm.fahrBox.val...
tempC = FahrToCelsius(tempF);
document.TempForm.celsiusBox.value = tempC;
}
function Convert(CtoF)
// Assumes: document.TempForm.fahrBox contains degrees Fahrenheit
// Results: assigns document.TempForm.celsiusBox the equiv. temperature
{
var tempC, tempF;
tempC = parseFloat(document.TempForm.fahrBox.val...
tempF = CelsiusToFahr(tempC);
document.TempForm.fahrBox.value = tempF;
}
</script>
</head>
<body>
<h2>Temperature Conversion Page</h2>
<hr />
<form name="TempForm">
Enter a temperature in degrees Fahrenheit:
<input type="text" name="fahrBox" size=10 value="" />
<input type="button" value="Convert to Celsius" onClick="Convert(FtoC);" />
Equivalent temperature in degrees Celsius:
<input type="text" name="celsiusBox" size=10 value Okay, the first problem is that you aren't familiar with function parameters.
With a declaration of:
Convert(FtoC), you are creating a function called convert, that takes one parameter and assigns it to variable 'FtoC'.
In fact, when your button onclick calls Convert(FtoC), it should throw a Javascript error since var FtoC is undefined.
Instead, skip the Convert functions altogether. Go straight to your FahrToCelsius and CelsiusToFahr straight off.
Also, you didn't write a CelsiusToFahr function yet.
Also, your Celsius to Fahrenheit equation is wrong.
Also, I really dislike the style of inserting comments between your function declaration and the beginning of the function block.
I did the liberty of correcting it all for you:
<html>
<!-- convert.html KG -->
<!-- This page converts temperatures from Fahrenheit to Celsius, and Celsius to Fahrenheit -->
<head>
<title>Temperature Conversion Page</title>
<script type="text/javascript">
function FahrToCelsius()
{
tempInFahr = parseFloat(document.TempForm.fahrBox.val...
document.TempForm.celsiusBox.value = (5/9) * (tempInFahr - 32);
}
function CelsiusToFahr()
{
tempInCels = parseFloat(document.TempForm.celsiusBox....
document.TempForm.fahrBox.value = (9/5) * tempInCels + 32;
}
</script>
</head>
<body>
<h2>Temperature Conversion Page</h2>
<hr />
<form name="TempForm">
Enter a temperature in degrees Fahrenheit:
<input type="text" name="fahrBox" size=10 value="" />
<input type="button" value="Convert to Celsius" onClick="FahrToCelsius();" />
Equivalent temperature in degrees Celsius:
<input type="text" name="celsiusBox" size=10 value="" />
<input type="button" value="Convert to Farenheit" onClick="CelsiusToFahr();" />
</form>
</body>
</html> Your second equation is wrong. You need to add the 32 AFTER you multiply by 9/5. Put the closing second parenthesis after tempinCels not after the 32. Im just a wee bit confused by your logic, or maybe its just Y! formatting buggering things up.
Im not a javascript programmer, but Ive never used a programming language that allowed multiple return statements the way you have your FahrToCelsius() function coded. That just seems wrong; not to mention the computation error pointed out in the other answer.
I hacked your original code and was able to make this bit of HTML/script work in my browser (Opera). You should be able to fix your code based on this:
<html>
<head>
<title>Temperature Conversion Page</title>
<script type="text/javascript">
function ConvertF() // convert degf to degc
{
var tempF;
tempF = parseFloat(document.TempForm.fahrBox.val... );
document.TempForm.celsiusBox.value = ((tempF-32) * 5)/9;
}
function ConvertC() // convert degc to degf
{
var tempC
tempC = parseFloat(document.TempForm.celsiusBox....
document.TempForm.fahrBox.value = (tempC * 1.8) + 32;
}
</script>
</head>
<body>
<h2>Temperature Conversion Page</h2>
<hr />
<form name="TempForm">
Enter a temperature in degrees Fahrenheit:
<input type="text" name="fahrBox" size=10 value="0" />
<input type="button" value="Convert to Celsius" onClick="ConvertF();" />
<p>
Equivalent temperature in degrees Celsius:
<input type="text" name="celsiusBox" size=10 value="0" />
<input type="button" value="Convert to Farenheit" onClick="ConvertC();" />
</form>
</body>
</html> |