Skip to main content
QB1Sho
Participant
June 14, 2019
Answered

I am trying to set up a javascript "If" formula on a form and keep running into errors.

  • June 14, 2019
  • 1 reply
  • 449 views

Good Morning/Afternoon!

I am trying to set up a calculation on a form that is based of an "if/Else" format.  The calculation in words would look something like this:

Skill Calculation:

If “Trained Skill 1”>0;

“bonus 1 = “Dex Mod” + “Trained Skill 1” + “Skill Misc Mod 1” + “Addtl Misc Mod 1”

Else

“bonus 1” = 0

If it were in excel it would look like this as far as format and expected outcome:

As you can see from the Excel snip, Row 2 shows that C1 ("Trained Skill 1") has a 1 in the column.  As a result, the sum of B2 ("Ability Mod"), C2 ("Trained Skill 1"), D2 ("Skill Misc Mod 1"), and E2 ("Addtl Misc Mod 1") is calculated and placed in A1 ("Bonus 1")

Conversely, Row 3 shows that C2 ( "Trained Skill 1") has a 0 in the column.  As a result, the sum of B2 ("Ability Mod"), C2 ("Trained Skill 1"), D2 ("Skill Misc Mod 1"), and E2 ("Addtl Misc Mod 1") is NOT calculated and a 0 is placed in B1 ("Bonus 1")

Can anyone help me create a javascript formula depicting this action?   I cant seem to get a working script.   

Thank you tremendously in advance!


Best!

JLD

This topic has been closed for replies.
Correct answer try67

You can use this code as the custom calculation script of "bonus 1":

if (Number(this.getField("Trained Skill 1").valueAsString)>0)

    event.value = Number(this.getField("Dex Mod").valueAsString) + Number(this.getField("Trained Skill 1").valueAsString) +

     Number(this.getField("Skill Misc Mod 1").valueAsString) + Number(this.getField("Addtl Misc Mod 1").valueAsString);

else event.value = 0;

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
June 14, 2019

You can use this code as the custom calculation script of "bonus 1":

if (Number(this.getField("Trained Skill 1").valueAsString)>0)

    event.value = Number(this.getField("Dex Mod").valueAsString) + Number(this.getField("Trained Skill 1").valueAsString) +

     Number(this.getField("Skill Misc Mod 1").valueAsString) + Number(this.getField("Addtl Misc Mod 1").valueAsString);

else event.value = 0;

QB1Sho
QB1ShoAuthor
Participant
June 14, 2019

Wow!  Such a fast response and right on the money!  Worked like a dream.  Thank you try67​ , you are a great asset to this community.  I was reading your past posts and was close...I just had a few missing things! 

Thank you a ton!  Have a great weekend!