/    Sign up×
Community /Pin to ProfileBookmark

Help with Calculators

I run a site/forum for Diabetes help. We’ve developed a Calculators block that helps convert Blood Sugar readings from one unit to another.

[CODE]<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8″>
<title>Diabetes Calculators</title>
<style>
p, label {
font-weight: normal;
display: block;
}
.sugar {
color: #00557F;
}
.a1c {
color: #AE0B1B;
}
.temperature {
color: #BA3114;
}
</style>
</head>
<body>
<div class=”sugar”>
<p>Blood Sugar</p>
<label><input id=”c” onkeyup=”convert(this,f)”size=”8″>&nbsp; mmol/l</label>
equals
<label><input id=”f” onkeyup=”convert(this,c)”size=”8″>&nbsp; mg/dl</label>
</div>

<div class=”a1c”>
<p>A1c to Avg. BG</p>
<label><input id=”a” onkeyup=”convert(this,s)” size=”8″>&nbsp; A1c</label>
equals
<label><input id=”s” onkeyup=”convert(this,a)”size=”8″>&nbsp; Est Avg BG</label>
</div>

<div class=”temperature”>
<p>Temperature</p>
<label><input id=”x” onkeyup=”convert(this,y)”size=”8″>&nbsp;<sup>o</sup> C</label>
equals
<label><input id=”y” onkeyup=”convert(this,x)”size=”8″>&nbsp;<sup>o</sup> F</label>
</div>

<script>
function convert(el,answer) {
switch (el.id) {
case ‘c’:
answer.value = (el.value * 18.05).toFixed(2);
break;
case ‘f’:
answer.value = (el.value / 18.05).toFixed(2);
break;
case ‘a’:
answer.value = ((el.value * 35.6) – 77.3).toFixed(2);
break;
case ‘s’:
answer.value = (((el.value * 1) + 77.3) / 35.6).toFixed(2);
break;
case ‘x’:
answer.value = ((el.value * 1.8) + 32) .toFixed(2);
break;
case ‘y’ :
answer.value = (((el.value * 1) -32) / 1.8) .toFixed(2);
break;
}
}
</script>

</body>
</html>
[/CODE]

The top 2 calculators work fine (when only the 2 are used)
As soon as the Temperature calculator is added all of them stop working. What am I doing wrong ?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@rootJan 07.2018 — I see

1. form inputs but no form

2. use of variables that are not defined

3. missing attributes on elements like name and value tags

4. script in the body not the head... generally speaking, code, unless for some reason for it, code should be placed within the head tag.

5. the error I get upon looking closer, I see the use of a character that is not a single quote, the main reason for using the " double quotes in a string is its not missed like a ' single quote or mistaken like this [B]&#8216; &#8217;[/B] as single quotes.

6. Remember DRY in all code, DRY = Dont Repeat Yourself, you have the possible use of a function to do repeated types of calculation...

function temp( scale , temp ){
try{
switch( scale.toLowerCase() ){
case "c2f":
return ( 9 / 5 ) * temp + 32;
break;

<i> </i> case "f2c":
<i> </i> return ( temp - 32 ) / ( 9 / 5 );
<i> </i> break;

<i> </i> default:
<i> </i> return temp;
<i> </i>}
}catch(e){ console.log("Error message: rnt%s",e); }
}


function convert( el ){
...
... other code here
...

<i> </i> case "x":
<i> </i>answer.value = temp( "c2f", el.value ) .toFixed(2);
<i> </i>break;
<i> </i>case "y":
<i> </i>answer.value = temp( "f2c", el.value ) .toFixed(2);
<i> </i>break;
<i> </i>}
}

The try..catch clause is to prevent divide by zero errors from happening.

and you could put in the missing bits... Its tidy code then

<input [B]name="c"[/B] id="c" onkeyup="convert(this,f)" size="8" [B]value=""[/B]>

I tested the temperature with -17.7777777777777 deg C and got 0 deg F. which is close enough I think.

So from this you should be able to update your code and correct the errors in the switch..case block.
Copy linkTweet thisAlerts:
@skbauthorJan 08.2018 — Thanks. Will implement and revert.
×

Success!

Help @skb 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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