1 person found this helpful
var scores = [+getField("BM1Score.0").value, +getField("BM2Score.0").value]; if (+getField("BM3Score.0").value > 0) scores.push(+getField("BM3Score.0").value); if (+getField("BM4Score.0").value > 0) scores.push(+getField("BM4Score.0").value); var average = scores.avg(); // Set this field value to the average event.value = average;
You need to work with the field values, which you get through the value property of a field object.
var score1 = this.getField("BM1Score.0");
var score2 = this.getField("BM2Score.0");
var score3 = this.getField("BM3Score.0");
var score4 = this.getField("BM4Score.0");
if(score3 > 0) scores.push(score3);
if(score4 > 0) scores.push(score4);
var average = scores.avg();
I'm kind of a crappy programmer, so bear with me. Once I decided to get the user input and convert it to a variable, I wasn't getting an error. I left out the print statement at the end, but it wasn't showing an average with my test values, but with the print statement it's still not showing a final output value.
I've been at this for hours and will still be going for a while, so any interim help would be appreciated. Thanks!
1 person found this helpful
If you want to create your own, you'll have to decide what you want to do if a field is blank. Should it be included in the average calculation or not?
First of all, you guys are amazing and thanks for the help so far!
My brain has turned to mush. It seems to be adding a value to the array? When I only have one or two values, it seems to be adding an extra zero value to the array. When I add a third value, it works. With a fourth value it works as well. It's putting in a zero for some reason which is screwing up the average.
var scores = [+getField("BM1Score.0").value, +getField("BM2Score.0").value];
if (+getField("BM3Score.0").value > 0) scores.push(+getField("BM3Score.0").value);
if (+getField("BM4Score.0").value > 0) scores.push(+getField("BM4Score.0").value);
avgNum = scores.length;
avgTotal = 0;
for(var i = 0; i < scores.length; i++)
avgTotal += scores[i];
var average = avgTotal / avgNum;
event.value = average;
The code that I posted will convert a blank value to zero. If you want to ignore blank values, the script would have to be modified.
It would help if you provided more information about how things are set up and how you want it to behave. It seems like you always want to include the first two fields. Is that what you want and will those fields always have a value (i.e., not blank)? Do you want to ignore blank fields?
I said my brain turned to mush. The code above works perfectly - it's solved.
Thank you all so much!
And yes, the first two fields always have a value, but the last two might not, which is why they weren't part of the array in the beginning. I need sleep. Thanks again.