12 Replies Latest reply on Jul 14, 2016 11:58 AM by Monteigo99

# Sum two fields then Divide by 2

(JJPC MCKTOTAL1 + JDPC MCKTOTAL1)/2

Can someone provide a Script for me to evaluate?

Thank you

• ###### 1. Re: Sum two fields then Divide by 2

You could put your calculation into the "Simplified Field Notation" method option.

If I remember correctly adding a number to itself and dividing by 2 will result in the number.

Have you looks at any of the free tutorials or videos on form creation?

• ###### 2. Re: Sum two fields then Divide by 2

If the field names contain spaces, though, you'll need to escape them, like this:

JJPC\ MCKTOTAL1

• ###### 3. Re: Sum two fields then Divide by 2

Thank you. The two fields are different; they have different names. I can't use the simplified field because it doesn't have secondary functions; e.g. it will add ONLY, but will not then divide.

I'm an advanced form creator, except when it comes to JavaScript.

I've searched all over the internet for this script, but all I get is simple tutorials that do not provide the structure of the entire syntax with all the {,},; etc. I don't know how to structure the script properly.

• ###### 4. Re: Sum two fields then Divide by 2

The basic structure of the script would be:

```var v1 = Number(this.getField("FieldName1").valueAsString);
var v2 = Number(this.getField("FieldName2").valueAsString);
event.value = (v1+v2)/2;
```
• ###### 5. Re: Sum two fields then Divide by 2

Not true. If you divide by a constant non-zero value it should work.

• ###### 6. Re: Sum two fields then Divide by 2

It worked! Thank you.

var v1 = Number(this.getField("JJPC_MCKTOTAL1").valueAsString);

var v2 = Number(this.getField("JDPC_MCKTOTAL1").valueAsString);

event.value = (v1+v2)/2;

• ###### 7. Re: Sum two fields then Divide by 2

So, the above script works but I just figured out that I need the event.value to only calculate if v2 is >0.  I tried this but it didn't work. Any assistance is appreciated.

var v1 =
Number(this.getField("JJPC_MCKTOTAL1").valueAsString);

var v2 =
Number(this.getField("JDPC_MCKTOTAL1").valueAsString);

event.value = If((v2>0),(v1+v2)/2,v1);

• ###### 8. Re: Sum two fields then Divide by 2

What should be the value of the field if that condition is not true?

• ###### 9. Re: Sum two fields then Divide by 2

If V2>0, then add v1+v2, if it is false, then v1.

Thanks

• ###### 10. Re: Sum two fields then Divide by 2

I've derived this formula that works for 5 of 6 fields. On the 6th field it is a one adjustment calculation delayed (I have 5 Y/N drop down list options that get calculated in a field, one for the first 6mths, then on another field for the second 6 mths).

What's wrong with this?

var v1 =
Number(this.getField("JJNotesCompletionRate").valueAsString);

var v2 =
Number(this.getField("JDNotesCompletionRate").valueAsString);

if(v2>0) {

event.value = (v1+v2)/2;

} else {

event.value = v1;}

• ###### 11. Re: Sum two fields then Divide by 2

Thr code is fine. The issue is with the Fields Calculation Order.