Computer problems,Computer help
*AX SOFT>>>Programming & Design

C to F, F to C, what is my mistake?


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>

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • PHP programming help using forms?

    that should work, if you are getting back the php then php is not configured properly, at least not where that file is located. But here is a very simple test, save this in the same place as a ....

  • Uploading Website help!!!?

    That is a server error. If you can use a local FTP to upload instead of GoDaddy's, i would try that way. A lot easier to use something like SmartFTP and set it up to connect. GoDaddy shou...

  • Visual Basic- StartingHelp?

    sure you can do it that way.. but you will need to make sure you are getting the floor of the change/2000... or you can use a series of while loops.. while intchange >= 2000 { intchange-= ...

  • Unix command helps!!?

    date -v -1d "+%D" >yesterday.txt (better read the man page to figure out why/how this works) ls -a /bin >~/temp/bin.txt

    ...
  • How do I make a groupbox border appear and darker and black?

    Basically the border code has a lot of border styles and some are pretty neat if the border size is set correctly. Like the style "inset": border: 5px inset #000; A simple border: ...

  • Adobe Photoshop Ghost Rider text recreate poster???

    Yes, you can create it in photoshop alone. No need additional images. 1. Steel effect can be achieved by gradient fill in blending options. Or, if your PS do not have blending option, just add a...

  • Changing text in a Div Box?

    Since the div has a class assigned to it, you refer to the class with CSS: <div class="Div1">HOW WOULD I CHANGE THIS TEXT???</div> <style type="text/css"> ...

  • Need help with Java programming (involving arrays)?

    Just write one yourself. It's easy. // array1 is already defined double total = 0; for( int i=0; i < array1.length; i++){ total+=array[i]; } double avg = total/array1.length;

    ...
  •  

    Categories--Copyright/IP Policy--Contact Webmaster