Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-03-2005, 08:29 PM   PM User | #1
jglover
New Coder

 
Join Date: Oct 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
jglover is an unknown quantity at this point
Large mathematical equation help

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 is offline   Reply With Quote
Old 03-03-2005, 08:35 PM   PM User | #2
jglover
New Coder

 
Join Date: Oct 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
jglover is an unknown quantity at this point
I think I may have figured it out, if you see something wrong please let me know.
jglover is offline   Reply With Quote
Old 03-03-2005, 11:19 PM   PM User | #3
jglover
New Coder

 
Join Date: Oct 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
jglover is an unknown quantity at this point
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>
jglover is offline   Reply With Quote
Old 03-03-2005, 11:23 PM   PM User | #4
jkd
Senior Coder

 
jkd's Avatar
 
Join Date: May 2002
Location: metro DC
Posts: 3,163
Thanks: 1
Thanked 18 Times in 18 Posts
jkd will become famous soon enough
someFormElement.value is always a String, not a Number. Use parseInt(someFormElement.value) or parseFloat to make it a Number.
__________________
jasonkarldavis.com
jkd is offline   Reply With Quote
Old 03-03-2005, 11:28 PM   PM User | #5
jglover
New Coder

 
Join Date: Oct 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
jglover is an unknown quantity at this point
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?
jglover is offline   Reply With Quote
Old 03-04-2005, 04:49 AM   PM User | #6
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
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.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 03-04-2005, 03:01 PM   PM User | #7
jglover
New Coder

 
Join Date: Oct 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
jglover is an unknown quantity at this point
Thank you very much, works like a charm!
jglover is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:31 AM.


Advertisement
Log in to turn off these ads.