/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Order of Operations correct, but script still not working.

Hello,

I’m new to Javascript, and I can’t figure out what is wrong with the following temperature conversion. When I enter 0 in to the celcius text box, the conversion for degrees Kelvin is wrong. It seems like an order of operations error, but I’m certain that they are correct.

The error is with this line:
form.kelvinbox.value = (form.celciusbox.value) + 273.15;
instead of adding the value of form.celciusbox.value (which is 0) to 273.15, it is combining the two as a number 0273.15. There is only one plus sign on my keyboard.

There is a similar error with degrees Kelvin when you enter a value in the Fahrenheit text box.

For reference 0 deg C = 273.15 K = 32 deg F

<html>
<head>
</head>
<body>

<script language=”javascript”>
function celciusconvert (form) {
form.fahrenheitbox.value = (form.celciusbox.value) * 9 / 5 + 32;
form.kelvinbox.value = (form.celciusbox.value) + 273.15;
}
function kelvinconvert (form) {
form.celciusbox.value = (form.kelvinbox.value) – 273.15;
form.fahrenheitbox.value = (form.kelvinbox.value) *
9 / 5 – 459.67;
}
function fahrenheitconvert (form) {
form.celciusbox.value = ((form.fahrenheitbox.value) – 32) * 5 / 9;
form.kelvinbox.value = ((form.fahrenheitbox.value) + 459.67) *
5 / 9;
}
</script>
<form>
Celsius: <input type=”text” name=”celciusbox” value=”” onkeyup=”celciusconvert(this.form)”><br />
Kelvin: <input type=”text” name=”kelvinbox” value=”” onkeyup=”kelvinconvert(this.form)”><br />
Fahrenheit: <input type=”text” name=”fahrenheitbox” value=”” onkeyup=”fahrenheitconvert(this.form)”><br />
<br />
<input type=”reset” name=”reset” value=”Reset”>
</form>

</body>
</html>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangNov 08.2010 — Input values are strings. Convert to the required type: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Functions#parseInt_and_parseFloat_Functions[CODE]form.kelvinbox.value = parseFloat(form.celciusbox.value) + 273.15;[/CODE]
Copy linkTweet thisAlerts:
@mrallaireauthorNov 08.2010 — Thank-you kindly Fang, it works now. ?
×

Success!

Help @mrallaire spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...