Click to See Complete Forum and Search --> : need help with javascript mathemathical formula


flen
12-16-2003, 09:48 AM
hello people,

I hope you will give me a little time to explain my problem to you.
I am a phycisian doing research in Rheumatology, who wants to build a webcalculator that calculates a patients disease activity score from clinical variables, like number of painfull joints and laboratory values. The formula has recently been developed and validated. When I try to incorporate it in a javascript webcalculator something goes wrong. The calculated values are higher than they should be.

this is the formula:
DAS28 (the name of the score) = 0.56*sqrt(A) + 0.28*sqrt(B) + 0.36*ln(C+1) + 0.014*D+ 0.96

I use a natural logarithm in the formula and the problem must be related to it, I think.

I have incorporated it into an existing javascript source and this is as far as I got:

<script language="JavaScript">
<!--
function round(number,x)
{x = (!x ? 2 : x);
return Math.round(number*Math.pow(10,x))/Math.pow(10,x);}

function calc (form)
{form.das28.value = round(0.555*(Math.sqrt(form.A.value))+0.284*(Math.sqrt(form.B.value))+Math.log((form.C.value)+1.0)*0 .36+form.D.value*0.0142+0.96)}
//-->
</script>



Fil in any number of A, B, C or D and this javascript calculates a number that is too high, except when C is zero. So I think I mistranslated the natural logarithm term (but I was ensured it has to be <log> instead of <ln>, is this correct?)

Hopefully this makes sense to anyone. I am an autodidact regarding webdesign/developement and its is probably a small problem that I cant solve because im am lacking the knowlegde of javascript, but I'm really on a dead end here. Hopefully some of you will give it some time and give me some suggestions/ideas on how to solve this.

Many thanks in advance,

Marcel Flendrie

(ps for examples of working webcalculators check out the website http://www.das-score.nl/www.das-score.nl/index.html)

bon
12-16-2003, 01:22 PM
Why do you have 0.7*log(c+1) in your script and 0.36*log(c+1) in your formula?

flen
12-17-2003, 02:42 AM
i'm sorry bon, my error. thank you for correcting it. correct formula should be 0.36, I have corrected it in my previous post. but then the difference still exists.
for instance when A, B, C and D are all 10 the script calculates 5,42, but the calculation should result in 4,62.

thanks bon, and greetings

Pittimann
12-17-2003, 03:15 AM
Hi!

Please try this:
form.das28.value = round(0.555*(Math.sqrt(form.A.value))+0.284*(Math.sqrt(form.B.value))+Math.log(parseInt(form.C.value )+1.0)*0.36+form.D.value*0.0142+0.96)

Cheers - Pit

flen
12-17-2003, 03:50 AM
Thanks Pittimann!

It works! I'll put it on the web this weekend, and i'll put your name in the credits.

many thanks!

Pittimann
12-17-2003, 03:51 AM
Hi!

You're welcome!

Cheers - Pit

flen
01-04-2004, 01:58 PM
hello again,

I've noticed another problem. The score is displayed in one decimal, but it is not rounded up. Could anyone give me a suggestion how to either round up the outcome to one decimal before displaying it or display the score in two decimals?
(Again, my knowlegde of javascript synthaxes is not very impressive but this calculator means a lot to me, so many thanks in advance for any help I get from you.)

Regards, M. Flendrie

Pittimann
01-04-2004, 02:20 PM
Hi!

I checked the site again and found (even though several things are different compared with last time) that the result is displayed with two decimals and rounded.

Difficult to give another hint, except this. It's an explanation of how rounding with javascript and setting the number of decimals can be done:

http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/number.html#1207032

Hope, that helps - cheers - Pit

flen
01-05-2004, 03:49 AM
thanks Pittiman,

You're right, somehow on my macintosh at home, it is displayed with one decimal, probably a wrong setting somewhere. on windows PC's it works fine.

I have updated the calculators, check em out.

Thanks again,
M. Flendrie