Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Average form values using only # of fields with data entered 1

Status
Not open for further replies.

fskipper

Programmer
Feb 26, 2004
10
US
Hi. I am trying to write a function that will take 15 form fields, sum the values and divide by all the non-zero values. For example: if 12 values aare entered I want the sum to be divided by 12 for the average.
Here's my cluncky code:


function sevavg() {
Sev = eval (document.Form.Sev.value);
Sev2 = eval (document.Form.Sev2.value);
Sev3 = eval (document.Form.Sev3.value);
Sev4 = eval (document.Form.Sev3.value);
Sev5 = eval (document.Form.Sev5.value);
Sev6 = eval (document.Form.Sev6.value);
Sev7 = eval (document.Form.Sev7.value);
Sev8 = eval (document.Form.Sev8.value);
Sev9 = eval (document.Form.Sev9.value);
Sev10 = eval (document.Form.Sev10.value);
Sev11 = eval (document.Form.Sev11.value);
Sev12 = eval (document.Form.Sev12.value);
Sev13 = eval (document.Form.Sev13.value);
Sev14 = eval (document.Form.Sev14.value);
Sev15= eval (document.Form.Sev15.value);

var myData = [document.Form.Sev.value,
document.Form.Sev2.value,
document.Form.Sev3.value,
document.Form.Sev3.value,
document.Form.Sev5.value,
document.Form.Sev6.value,
document.Form.Sev7.value,
document.Form.Sev8.value,
document.Form.Sev9.value,
document.Form.Sev10.value,
document.Form.Sev11.value,
document.Form.Sev12.value,
document.Form.Sev13.value,
document.Form.Sev14.value,
document.Form.Sev15.value];





var alen = myData.length;
document.Form.sevavg.value = (Sev+Sev2+Sev3+Sev4+Sev5+Sev6+Sev7+Sev8+Sev9+Sev10+Sev11+Sev12+Sev13+Sev14+Sev15)/alen;

}
</script>
 
function sevavg(){
numVals = 0
ttlVal = 0
for (x=1: x<=15; x++){
if (x=1){
if (! isNaN(parseInt(document.Form.Sev.value))){
numVals ++
ttlVal += parseInt(document.Form.Sev.value)
}
}
else{
if (! isNaN(parseInt(eval("document.Form.Sev" + x + ".value")))){
numVals ++
ttlVal += parseInt(eval("document.Form.Sev" + x + ".value")
}
}
}
if (numVals <> 0){
alert ("average is " + ttlVal / numVals)
}

}

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
try this...a bit sloppy, but it works.

<HTML>
<HEAD>
<TITLE>javascript test</TITLE>

<script>

function avg() {

d=document.all;

b1=d.box1.value;
b2=d.box2.value;
b3=d.box3.value;
b4=d.box4.value;
b5=d.box5.value;
b6=d.box6.value;

x=6;//total number of fields

if (b1=='') {b1=0;x--} else {b1=parseFloat(b1);}
if (b2=='') {b2=0;x--} else {b2=parseFloat(b2);}
if (b3=='') {b3=0;x--} else {b3=parseFloat(b3);}
if (b4=='') {b4=0;x--} else {b4=parseFloat(b4);}
if (b5=='') {b5=0;x--} else {b5=parseFloat(b5);}
if (b6=='') {b6=0;x--} else {b6=parseFloat(b6);}

nTotal=b1+b2+b3+b4+b5+b6;
nAvg=nTotal/x;

d.avg.value=nAvg;

}

</script>

</HEAD>
<BODY>

<input type=text size=4 name=box1><br>
<input type=text size=4 name=box2><br>
<input type=text size=4 name=box3><br>
<input type=text size=4 name=box4><br>
<input type=text size=4 name=box5><br>
<input type=text size=4 name=box6><br>
<input type=button value=Average onclick=avg();><br>
<input type=text size=9 name=avg><br>

</BODY>
</HTML>

hope that helps...

-g
 
I tried the code that mwolf00 sent. I modified it a bit to fix some minor syntax problems. When I call the fucntion the browser freezes like an endless loop has been created. Can you see any errors here?


function sevavg(){
numVals = 0;
ttlVal = 0;
for (x=1; x<=15; x++){
if (x=1){
if (!isNaN(parseInt(document.Form.Sev.value))){
numVals ++;
ttlVal += parseInt(document.Form.Sev.value);
}
}
else{
if (!isNaN(parseInt(eval("document.Form.Sev" + x + ".value")))){
numVals ++;
ttlVal += parseInt(eval("document.Form.Sev" + x + ".value"));
}
}
}

if (numVals != 0){document.Form.sevavg.value= (ttlval / numVals);
// alert ("average is " + ttlVal / numVals);

}
 
remove the brace from this line...
Code:
if (numVals != 0)[s]{[/s]document.Form.sevavg.value= (ttlval / numVals);

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
I removed the brace and tried it, then the closing brace (I think I truncated one closer above) still no-go. The code looks like it should work.:

function sevavg(){
numVals = 0;
ttlVal = 0;
for (x=1; x<=15; x++){
if (x=1){
if (!isNaN(parseInt(document.Form.Sev.value))){
numVals ++;
ttlVal += parseInt(document.Form.Sev.value);
}
}
else{
if (!isNaN(parseInt(eval("document.Form.Sev" + x + ".value")))){
numVals ++;
ttlVal += parseInt(eval("document.Form.Sev" + x + ".value"));
}
}
}

if (numVals != 0)
document.Form.sevavg.value= (ttlval / numVals);
// alert ("average is " + ttlVal / numVals);

}
 
what kind of error are you getting?

BTW - I try not to name functions and form fields the same thing - it can get confusing

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
Thanks for the tip. I can see your point (function names).
I renamed the function 'severityAverage()". Here's what happens: On the form the 15 "sev" values are actually entered into the forms by another Javascript. There are three form fields per Sev field. A calculation is run that adds two of the fields and multiplies by the third field upon which that calc'd value is entered into its respective Sev field. That part works great. However when I click a link which has an onClick event to run the severityAverage() function, all browser activity stops. I just get the 'hand'. I can't click any links or even close the window unless I end the process through task manager. If I leave it runnung I get a message that says'A script is running that is taking up too much memory. Click to stop script.' In my experience, that means an endless loop. I can't see where that is happeneing here unless the counter 'x' is trying to reach 15 but never does because all 15 sev fields aren't filled in. heres the code again just in case.
function severityAverage(){
numVals = 0;
ttlVal = 0;
for (x=1; x<=15; x++){
if (x=1){
if (!isNaN(parseInt(document.Form.Sev.value))){
numVals ++;
ttlVal += parseInt(document.Form.Sev.value);
}
}
else{
if (!isNaN(parseInt(eval('document.Form.Sev' + x + '.value')))){
numVals ++;
ttlVal += parseInt(eval('document.Form.Sev' + x + '.value'));
}
}
}

if (numVals != 0){
document.Form.sevavg.value= (ttlval / numVals);
// alert ("average is " + ttlVal / numVals);
}
}
 
DOH!!!!

if (x=1) keeps making x = 1

Should be
Code:
  if (x==1){

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
Sweet. Thanks. I should have noticed that! At least I am finally getting ananswer. Now I actually get an answer in the sevavg field, but no matter how many sev's are entered it divides by 15 ( the max number of sev's).
 
OH, BTW. I have the intial values of the Sev fields set to '0'.
 
Then change the conditions...

if (!isNaN(parseInt(eval('document.Form.Sev' + x + '.value'))) && eval('document.Form.Sev' + x + '.value') != "0" ){


Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
Got It!!!! Thanks! I changed the intital value to null and it works. I appreciate the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top