Skip to main content
Participant
Nov 29, 2018 1:57 PM UTC
Answered

Calculation with Minimum Value

How do I write a custom calculation for:

Text1 + Text2 + Text3 = Text20

If sum is 0 then value is 0

If sum is any number between 1 - 300 then value is 300

If sum is >300 then value is sum

Here is what is NOT working:

if( Text20 <= 300 ) {

event.value = 300; // value is 300 when sum is less than or equal to 300;

} else {

if ( Text20 == 0 ) {

event.value == 0; // value is 0 when sum is equal to 0;

} else {

event.value = Text20; // else value is the sum;

}

}

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Nov 29, 2018 2:58 PM UTC

Use this code as the custom calculation script of your field:

var total = Number(this.getField("Text1").valueAsString) + Number(this.getField("Text2").valueAsString) + Number(this.getField("Text3").valueAsString);

if (total>1 && total<300) total = 300;

event.value = total;

2 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Nov 29, 2018 2:58 PM UTC

Use this code as the custom calculation script of your field:

var total = Number(this.getField("Text1").valueAsString) + Number(this.getField("Text2").valueAsString) + Number(this.getField("Text3").valueAsString);

if (total>1 && total<300) total = 300;

event.value = total;

Translate
Legend
Nov 29, 2018 2:15 PM UTC

Looks about right, except you are missing the compulsory "getField" call.

Translate