...

View Full Version : Large mathematical equation help



jglover
03-03-2005, 08:29 PM
Hello,

I have a script I need to run a large equation for. I'm guessing my style of the math is wrong. Here is what I have, any help would be so greatly appreciated.

Piece of code with trouble:
hwfcfp=(form1.lean6.value*((100-form1.fat6.value)/100)+form1.lean5.value*((100-form1.fat5.value)/100)+form1.lean4.value*((100-form1.fat4.value)/100)+form1.lean3.value*((100-form1.fat3.value)/100)+form1.lean2.value*((100-form1.fat2.value)/100)+form1.lean1.value*((100-form1.fat1.value)/100));
form1.hwfcfp.value=hwfcfp.toFixed(2)

and if you need it, here is the rest of the page:

<HTML>
<Head>
<TITLE>
Pre-Rigor Fat Correction Worksheet
</TITLE>
<Script Language=JavaScript>


function calcForm(isForm){

var objForm=document.form1;
var arrToCalc=new Array("lean1","lean2", "lean3", "lean4", "lean5", "lean6");

var sum=0;
var total=0;
var curValue;

for (var i=0; i<arrToCalc.length; i++)
{
curValue=eval("objForm."+arrToCalc[i]).value;
if ((curValue.length == 0) || (isNaN(curValue)))
{
//empty or not numeric...
}
else
{
sum += (curValue*(-1)*(-1));
total++;
}
}
objForm.tlweight.value = sum+"";

isSum = sum;

salt1 = (form1.lean1.value)*.015;
form1.salt1.value=salt1.toFixed(2);

salt2 = (form1.lean2.value)*.015;
form1.salt2.value=salt2.toFixed(2);

salt3 = (form1.lean3.value)*.015;
form1.salt3.value=salt3.toFixed(2);

salt4 = (form1.lean4.value)*.015;
form1.salt4.value=salt4.toFixed(2);

salt5 = (form1.lean5.value)*.015;
form1.salt5.value=salt5.toFixed(2);

salt6 = (form1.lean6.value)*.015;
form1.salt6.value=salt6.toFixed(2);

hwfcfp=(form1.lean6.value*((100-form1.fat6.value)/100)+form1.lean5.value*((100-form1.fat5.va

lue)/100)+form1.lean4.value*((100-form1.fat4.value)/100)+form1.lean3.value*((100-form1.fat3.

value)/100)+form1.lean2.value*((100-form1.fat2.value)/100)+form1.lean1.value*((100-form1.fat

1.value)/100));
form1.hwfcfp.value=hwfcfp.toFixed(2)

}

</Script>
</Head>
<Body>
<body bgcolor="ffffff">
<form method="POST" action="formkill.asp" name="form1">
<H1 align=center><B>Pre-Rigor Fat Correction Worksheet</B></H1>

<table>
<tr><td>Lean Weight (lbs):</td><td><INPUT TYPE="text" NAME="lean1" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Fat Percentage</td><td><INPUT TYPE="text" NAME="fat1" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Salt Weight to add</td><td><INPUT TYPE="text" NAME="salt1" SIZE="4"

onkeyup="calcForm();"></td></tr>
</table>

<br>

<table>
<tr><td>Lean Weight (lbs):</td><td><INPUT TYPE="text" NAME="lean2" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Fat Percentage</td><td><INPUT TYPE="text" NAME="fat2" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Salt Weight to add</td><td><INPUT TYPE="text" NAME="salt2" SIZE="4"

onkeyup="calcForm();"></td></tr>
</table>

<br>

<table>
<tr><td>Lean Weight (lbs):</td><td><INPUT TYPE="text" NAME="lean3" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Fat Percentage</td><td><INPUT TYPE="text" NAME="fat3" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Salt Weight to add</td><td><INPUT TYPE="text" NAME="salt3" SIZE="4"

onkeyup="calcForm();"></td></tr>
</table>

<br>

<table>
<tr><td>Lean Weight (lbs):</td><td><INPUT TYPE="text" NAME="lean4" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Fat Percentage</td><td><INPUT TYPE="text" NAME="fat4" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Salt Weight to add</td><td><INPUT TYPE="text" NAME="salt4" SIZE="4"

onkeyup="calcForm();"></td></tr>
</table>

<br>

<table>
<tr><td>Lean Weight (lbs):</td><td><INPUT TYPE="text" NAME="lean5" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Fat Percentage</td><td><INPUT TYPE="text" NAME="fat5" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Salt Weight to add</td><td><INPUT TYPE="text" NAME="salt5" SIZE="4"

onkeyup="calcForm();"></td></tr>
</table>

<br>

<table>
<tr><td>Lean Weight (lbs):</td><td><INPUT TYPE="text" NAME="lean6" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Fat Percentage</td><td><INPUT TYPE="text" NAME="fat6" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Salt Weight to add</td><td><INPUT TYPE="text" NAME="salt6" SIZE="4"

onkeyup="calcForm();"></td></tr>

<tr><td></td><td>
Desired Lean Percentage</td><td><INPUT TYPE="text" NAME="dlean" SIZE="4"

STYLE="background-color: yellow;" VALUE="71" onkeyup="calcForm();"></td></tr>
<tr><td></td><td>
Desired Salt Percentage</td><td><INPUT TYPE="text" NAME="dsalt" SIZE="4"

STYLE="background-color: yellow;" VALUE="1.5" onkeyup="calcForm();"></td></tr>
<tr><td></td><td>
Desired Water Percentage</td><td><INPUT TYPE="text" NAME="dwater" SIZE="4"

STYLE="background-color: yellow;" VALUE="0" onkeyup="calcForm();"></td></tr>
<tr><td></td><td>
Fat Correction Fat Percentage (FCFP)</td><td><INPUT TYPE="text" NAME="fcfp" SIZE="4"

STYLE="background-color: yellow;"VALUE="23" onkeyup="calcForm();"></td></tr>

<tr><td></td><td></td><td></td><td></td><td>Total Weight</td><td>Percent Lean</td></tr>


<tr><td></td>
<td>Total Lean Weight</td><td><INPUT TYPE="text" NAME="htlweight" SIZE="4"

STYLE="background-color: yellow;" onkeyup="calcForm();"></td>
<td></td><td><INPUT TYPE="text" NAME="tlweight" SIZE="4" STYLE="background-color: yellow;"

onkeyup="calcForm();"></td>
<td><INPUT TYPE="text" NAME="tleanpercent" SIZE="4" STYLE="background-color: yellow;"

onkeyup="calcForm();"></td></tr>

<tr><td></td>
<td>Weight to add at F.C.F.P.</td><td><INPUT TYPE="text" NAME="hwfcfp" SIZE="4"

STYLE="background-color: yellow;" onkeyup="calcForm();"></td>
<td></td><td><INPUT TYPE="text" NAME="tfcfp" SIZE="4" STYLE="background-color: yellow;"

onkeyup="calcForm();"></td>
<td><INPUT TYPE="text" NAME="tfcfppercent" SIZE="4" STYLE="background-color: yellow;"

onkeyup="calcForm();"></td></tr>

<tr><td></td>
<td>Salt to add woth fat (lbs)</td><td></td>
<td></td><td><INPUT TYPE="text" NAME="tsalt" SIZE="4" STYLE="background-color: yellow;"

onkeyup="calcForm();"></td></tr>

</table>


<input type="reset" name="Reset" value="Reset">
</form>

</body>
</html>

jglover
03-03-2005, 08:35 PM
I think I may have figured it out, if you see something wrong please let me know.

jglover
03-03-2005, 11:19 PM
Ok Everything seems to be working except one equation and I've been working on it for 3 hours. This project is pretty much just an excel spreadsheet carried over. I used the same formulas from the excel spreadsheet and they all work but this one. PLEASE HELP!

tpercent=(form1.hwfcfp.value+form1.htlweight.value)/(form1.tfcfp.value+form1.tlweight.value);
form1.tpercent.value=tpercent.toFixed(2);

All of code:

<HTML>
<Head>
<TITLE>
Pre-Rigor Fat Correction Worksheet
</TITLE>
<Script Language=JavaScript>


function calcForm(isForm){

var objForm=document.form1;
var arrToCalc=new Array("lean1","lean2", "lean3", "lean4", "lean5", "lean6");

var sum=0;
var total=0;
var curValue;


for (var i=0; i<arrToCalc.length; i++)
{
curValue=eval("objForm."+arrToCalc[i]).value;
if ((curValue.length == 0) || (isNaN(curValue)))
{
//empty or not numeric...
}
else
{
sum += (curValue*(-1)*(-1));
total++;
}
}
objForm.tlweight.value = sum+"";

isSum = sum;

salt1 = (form1.lean1.value)*.015;
form1.salt1.value=salt1.toFixed(2);

salt2 = (form1.lean2.value)*.015;
form1.salt2.value=salt2.toFixed(2);

salt3 = (form1.lean3.value)*.015;
form1.salt3.value=salt3.toFixed(2);

salt4 = (form1.lean4.value)*.015;
form1.salt4.value=salt4.toFixed(2);

salt5 = (form1.lean5.value)*.015;
form1.salt5.value=salt5.toFixed(2);

salt6 = (form1.lean6.value)*.015;
form1.salt6.value=salt6.toFixed(2);

htlweight=(form1.lean6.value*((100-form1.fat6.value)/100)+form1.lean5.value*((100-form1.fat5

.value)/100)+form1.lean4.value*((100-form1.fat4.value)/100)+form1.lean3.value*((100-form1.fa

t3.value)/100)+form1.lean2.value*((100-form1.fat2.value)/100)+form1.lean1.value*((100-form1.

fat1.value)/100));
form1.htlweight.value=htlweight.toFixed(2);



tleanpercent=form1.htlweight.value/form1.tlweight.value*100;
form1.tleanpercent.value=tleanpercent.toFixed(2);

tfcfp=(form1.htlweight.value-((form1.dlean.value/100)*form1.tlweight.value))/((form1.dlean.v

alue/100)-((form1.fcfp.value)/100));
form1.tfcfp.value=tfcfp.toFixed(2);

hwfcfp=form1.tfcfp.value*(form1.fcfp.value)/100;
form1.hwfcfp.value=hwfcfp.toFixed(2);



tpercent=(form1.hwfcfp.value+form1.htlweight.value)/(form1.tfcfp.value+form1.tlweight.value)

;
form1.tpercent.value=tpercent.toFixed(2);


tsalt=(form1.tfcfp.value*.015);
form1.tsalt.value=tsalt.toFixed(2);

}

</Script>
</Head>
<Body>
<body bgcolor="ffffff">
<form method="POST" action="formkill.asp" name="form1">
<H1 align=center><B>Pre-Rigor Fat Correction Worksheet</B></H1>

<table>
<tr><td>Lean Weight (lbs):</td><td><INPUT TYPE="text" NAME="lean1" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Fat Percentage</td><td><INPUT TYPE="text" NAME="fat1" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Salt Weight to add</td><td><INPUT TYPE="text" NAME="salt1" SIZE="4"

onkeyup="calcForm();"></td></tr>
</table>

<br>

<table>
<tr><td>Lean Weight (lbs):</td><td><INPUT TYPE="text" NAME="lean2" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Fat Percentage</td><td><INPUT TYPE="text" NAME="fat2" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Salt Weight to add</td><td><INPUT TYPE="text" NAME="salt2" SIZE="4"

onkeyup="calcForm();"></td></tr>
</table>

<br>

<table>
<tr><td>Lean Weight (lbs):</td><td><INPUT TYPE="text" NAME="lean3" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Fat Percentage</td><td><INPUT TYPE="text" NAME="fat3" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Salt Weight to add</td><td><INPUT TYPE="text" NAME="salt3" SIZE="4"

onkeyup="calcForm();"></td></tr>
</table>

<br>

<table>
<tr><td>Lean Weight (lbs):</td><td><INPUT TYPE="text" NAME="lean4" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Fat Percentage</td><td><INPUT TYPE="text" NAME="fat4" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Salt Weight to add</td><td><INPUT TYPE="text" NAME="salt4" SIZE="4"

onkeyup="calcForm();"></td></tr>
</table>

<br>

<table>
<tr><td>Lean Weight (lbs):</td><td><INPUT TYPE="text" NAME="lean5" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Fat Percentage</td><td><INPUT TYPE="text" NAME="fat5" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Salt Weight to add</td><td><INPUT TYPE="text" NAME="salt5" SIZE="4"

onkeyup="calcForm();"></td></tr>
</table>

<br>

<table>
<tr><td>Lean Weight (lbs):</td><td><INPUT TYPE="text" NAME="lean6" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Fat Percentage</td><td><INPUT TYPE="text" NAME="fat6" SIZE="4"

onkeyup="calcForm();"></td></tr><tr>
<td>Salt Weight to add</td><td><INPUT TYPE="text" NAME="salt6" SIZE="4"

onkeyup="calcForm();"></td></tr>

<tr><td></td><td>
Desired Lean Percentage</td><td><INPUT TYPE="text" NAME="dlean" SIZE="4"

STYLE="background-color: yellow;" VALUE="71" onkeyup="calcForm();"></td></tr>
<tr><td></td><td>
Desired Salt Percentage</td><td><INPUT TYPE="text" NAME="dsalt" SIZE="4"

STYLE="background-color: yellow;" VALUE="1.5" onkeyup="calcForm();"></td></tr>
<tr><td></td><td>
Desired Water Percentage</td><td><INPUT TYPE="text" NAME="dwater" SIZE="4"

STYLE="background-color: yellow;" VALUE="0" onkeyup="calcForm();"></td></tr>
<tr><td></td><td>
Fat Correction Fat Percentage (FCFP)</td><td><INPUT TYPE="text" NAME="fcfp" SIZE="4"

STYLE="background-color: yellow;"VALUE="23" onkeyup="calcForm();"></td></tr>

<tr><td></td><td></td><td></td><td></td><td>Total Weight</td><td>Percent Lean</td></tr>


<tr><td></td>
<td>Total Lean Weight</td><td><INPUT TYPE="text" NAME="htlweight" SIZE="4"

STYLE="background-color: yellow;" onkeyup="calcForm();"></td>
<td></td><td><INPUT TYPE="text" NAME="tlweight" SIZE="4" STYLE="background-color: yellow;"

onkeyup="calcForm();"></td>
<td><INPUT TYPE="text" NAME="tleanpercent" SIZE="4" STYLE="background-color: yellow;"

onkeyup="calcForm();"></td></tr>

<tr><td></td>
<td>Weight to add at F.C.F.P.</td><td><INPUT TYPE="text" NAME="hwfcfp" SIZE="4"

STYLE="background-color: yellow;" onkeyup="calcForm();"></td>
<td></td><td><INPUT TYPE="text" NAME="tfcfp" SIZE="4" STYLE="background-color: yellow;"

onkeyup="calcForm();"></td>
<td><INPUT TYPE="text" NAME="tpercent" SIZE="4" STYLE="background-color: yellow;"

onkeyup="calcForm();"></td></tr>

<tr><td></td>
<td>Salt to add with fat (lbs)</td><td></td>
<td></td><td><INPUT TYPE="text" NAME="tsalt" SIZE="4" STYLE="background-color: yellow;"

onkeyup="calcForm();"></td></tr>

</table>


<input type="reset" name="Reset" value="Reset">
</form>

</body>
</html>

jkd
03-03-2005, 11:23 PM
someFormElement.value is always a String, not a Number. Use parseInt(someFormElement.value) or parseFloat to make it a Number.

jglover
03-03-2005, 11:28 PM
I did this:

tpercent=parseInt(form1.hwfcfp.value+form1.htlweight.value)/parseInt(form1.tfcfp.value+form1.tlweight.value);

got #'s now just not right ones :)

New to this, never used parsInt before.

Any Ideas?

glenngv
03-04-2005, 04:49 AM
You need to parseInt each value separately

tpercent = (parseInt(form1.hwfcfp.value) + parseInt(form1.htlweight.value))/(parseInt(form1.tfcfp.value) + parseInt(form1.tlweight.value));

If you expect decimal numbers, use parseFloat instead of parseInt.

jglover
03-04-2005, 03:01 PM
Thank you very much, works like a charm!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum