So I just enrolled in a JavaScript class and I’m getting stuck with a small problem with my [b]Fahrenheit to Celsius
[i]To convert F to C, you subtract 32 from the F and multiply the remainder by .55.
[i]To convert C to F, you multiply the temp by 1.8 and add 32.
Seems simple enough, yet I’m having problems with the F to C function [b]temp2(form)
You can view the page live here:
[url]http://sfbaywebsites.com/js/ConvertTemperature.html
[b][size=3]The code
[code]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” lang=”en” xml:lang = “en” dir=”ltr”>
<head>
<title>Covert Temperature</title>
</head>
<script language=”Javascript”>
<!–
function temp (form) {
form.fahrenheit.value = form.celsius.value*1.8+32
}
function temp2 (form) {
form.fahrenheit.value = form.celsius.value-32*.55
}
//–>
</script>
<body>
<table cellpadding=”0″ cellspacing=”0″ style=”width: 100%”>
<tr>
<td style=”width: 80%” valign=”top”>
<form>
<input name=”celsius” size=”15″ type=”text”> <b>Degrees Celsius</b>
<input onclick=”temp(this.form)” type=”button” value=” = “>
<input name=”fahrenheit” size=”15″ type=”text”> <b>Degrees Fahrenheit</b>
</form>
</td>
</tr>
</table>
<div style=”margin-top:50px;”>
<table cellpadding=”0″ cellspacing=”0″ style=”width: 100%”>
<tr>
<td style=”width: 80%” valign=”top”>
<form>
<input name=”celsius” size=”15″ type=”text”> <b>Degrees Fahrenheit</b>
<input onclick=”temp2(this.form)” type=”button” value=” = “>
<input name=”fahrenheit” size=”15″ type=”text”> <b>Degrees Celsius</b>
</form>
</td>
</tr>
</table>
</div>
</body>
</html>
I’m not sure if I’m supposed to subtract 32 and convert 0.55 into a fraction or what. Any help would be grateful. Thanks.
[code=html]form.fahrenheit.value = form.celsius.value-32*.55[/code]
[code=html]form.fahrenheit.value = (form.celsius.value-32)*.55[/code]
1: Place your script-tag inside the HEAD-element, not between the HEAD and the BODY.[/QUOTE]
5: It is always a good idea to finish your assignments with a semi-colon ;[/QUOTE]
4: Your error is not mathmatical, but logical. Take a close loo at the content of your 2 functions:
form.fahrenheit.value = form.celsius.value*1.8+32
form.fahrenheit.value = form.celsius.value-32*.55
I have a hunch the last one should be
form.celsius.value = form.fahrenheit.value-32*.55;[/QUOTE]
2: Cool guys add type="text/javascript" in the script-declaration[/QUOTE]
[CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang = "en" dir="ltr">
<head>
<title>Covert Temperature</title>
<script type="text/javascript" language="Javascript">
function temp (form) {
form.fahrenheit.value = (form.celsius.value*1.8)+32;
}
function temp2 (form) {
form.celsius.value = (form.fahrenheit.value-32)*0.55;
}
</script>
</head>
<body>
<table cellpadding="0" cellspacing="0" style="width: 100%">
<tr>
<td style="width: 80%" valign="top">
<form>
<input name="celsius" size="15" type="text"/> <b>Degrees Celsius</b>
<input onclick="temp(this.form)" type="button" value=" = "/>
<input name="fahrenheit" size="15" type="text"/> <b>Degrees Fahrenheit</b>
</form>
</td>
</tr>
</table>
<div style="margin-top:50px;">
<table cellpadding="0" cellspacing="0" style="width: 100%">
<tr>
<td style="width: 80%" valign="top">
<form>
<input name="fahrenheit" size="15" type="text"/> <b>Degrees Fahrenheit</b>
<input onclick="temp2(this.form)" type="button" value=" = "/>
<input name="celsius" size="15" type="text"/> <b>Degrees Celsius</b>
</form>
</td>
</tr>
</table>
</div>
</body>
</html>[/CODE]
That's actually a pet-peeve of mine. The language attribute is becoming outdated. As spinner suggets and the XHTML doctype requires, use [I]type='text/javascript'[/I] instead.[/QUOTE]
And as the XHTML doctype is also now outdated, there is no need to garnish the script tag with attributes since, despite Mr Gates' best efforts, what alternative is there to javascript?:p[/QUOTE]
The XHTML doctype is outdated? I was unaware ... Inform webdeveloper.com at once![/QUOTE]
I think he means the Transitional part of it ?[/QUOTE]
function temp2 (form) {
form.celsius.value = ((form.fahrenheit.value-32)/1.8).toFixed(2);
}
That will solve all your problems.You could also just multiply by 0.55556 or something like that too, but I prefer dividing by 1.8.[/QUOTE]
Given that it was never possible to send XHTML to the majority of web-users, to say it was even "in" is misleading to say the least.[/QUOTE]
1.8 / 1 = 0.55555 ... Given the fuzzy nature of division and the fact that you can't get more than 2 significant digits from a temperature calculation, multiplying by .55 is just as good.[/quote]
... [I][B]?![/B][/I] I'm having a tough time thinking of a browser that struggles with XHTML.[/quote]I hope you are joking. Internet Explorer does not support XHTML whatsoever. Simply putting a XHTML doctype on a page does not make it parsed as XHTML, so you get absolutely no benefit. In fact, if you use an XML declaration, which admittedly is not required, merely recommended, your page will be parsed in quirks mode in IE. I simply find it ridiculous to pretend to use XHTML when in fact it is always parsed as HTML in IE, and most likely in all other browsers too (unless you specifically send the appropriate header).
Internet Explorer does not support XHTML whatsoever.[/QUOTE]
And a reason to change? Hopefully HTML 5 in the near future.[/QUOTE]
OK, seconds out!
It was a flippant comment - I have an XHTML book right in front of me on my desk here - bought new all of 10 years ago.
I think we can agree by now that it is not a universally accepted standard ?[/QUOTE]
On the contrary, IE has supported XHTML 1.0 Strict ("with system identifier and without an XML declaration") is the [B]one of three[/B] doctype that IE has supported in non-quirks mode since [I]Mac version 5[/I].[/QUOTE]
Given that it was never possible to send XHTML to the majority of web-users, to say it was even "in" is misleading to say the least. Just because you put an XHTML doctype on a page, does not make it XHTML, it simply makes it invalid HTML unless served with an XHTML mime-type.[/QUOTE]
Alright, I lied[/quote]So will I, but just to make peace
I get what you're saying.[/quote]I'm happy with that, even if we disagree. HTML 4.01 and XHTML 1.0 are virtually the same doctype as far as modern browsers are concerned, I find it silly to use the latter.
And I care that doctype has no relevance to the OP.[/QUOTE]
So will I, but just to make peace
While I am certainly guilty, in fairness, you posted irrelevant material first, with no relevant material.[/QUOTE]
His form field and variable names are stupid. (sorry tommy)[/QUOTE]
[code=html]form.fahrenheit.value = form.celsius.value-32*.55[/code]
looks like it should be
[code=html]form.fahrenheit.value = (form.celsius.value-32)*.55[/code]
[/QUOTE]
You've been given incorrect data, you're code is now perfect. The conversion rate is to divide by 1.8, which is .5555555..., which is where the inaccuracy arises.That will solve all your problems.
function temp2 (form) {
form.celsius.value = ((form.fahrenheit.value-32)/1.8).toFixed(2);
}
[/QUOTE]
All that does it make the page invalid as far as WC3 is concerned. It will still display perfectly fine.[/QUOTE]
0.1.9 — BETA 5.18