Trying to calculate a text field, but getting NaN and "[object Field]0"

New Here ,
Aug 25, 2022 Aug 25, 2022

Copy link to clipboard

Copied

Hello i have a two dropdown fields, two text fields with values, and a text field with a custom calculation script.

 

I have this in the text field:

var class1 = this.getField("Class1");
var class2 = this.getField("Class2");
var level1 = this.getField("Level1");
var level2 = this.getField("Level2");

if ((class1.value==="Bard")||(class1.value==="Cleric")||(class1.value==="Druid")||(class1.value==="Sorcerer")||(class1.value==="Wizard"))
    {
    var a = level1;
    if ((class2.value==="Bard")||(class2.value==="Cleric")||(class2.value==="Druid")||(class2.value==="Sorcerer")||(class2.value==="Wizard")) {var b = level2;}
    else if ((class2.value==="Paladin")||(class2.value==="Ranger")) {var b = Math.floor(level2 / 2);}
    else if (class2.value==="Artificer") {var b = Math.ceil(level2 / 2);}
    else if ((class2.value==="Fighter")||(class2.value==="Rogue")) {var b = Math.floor(level2 / 3);}
    else{var b = 0;}
    var spellCasterLevel = a + b;
    event.value = spellCasterLevel;
    }
else if ((class1.value==="Paladin")||(class1.value==="Ranger"))
    {
    var a = Math.floor(level1 / 2);
    if ((class2.value==="Bard")||(class2.value==="Cleric")||(class2.value==="Druid")||(class2.value==="Sorcerer")||(class2.value==="Wizard")) {var b = level2;}
    else if ((class2.value==="Paladin")||(class2.value==="Ranger")) {var b = Math.floor(level2 / 2);}
    else if (class2.value==="Artificer") {var b = Math.ceil(level2 / 2);}
    else if ((class2.value==="Fighter")||(class2.value==="Rogue")) {var b = Math.floor(level2 / 3);}
    else{var b = 0;}
    var spellCasterLevel = a + b;
    event.value = spellCasterLevel;
    }
else if (class1.value==="Artificer")
    {
    var a = Math.ceil(level1 / 2);
    if ((class2.value==="Bard")||(class2.value==="Cleric")||(class2.value==="Druid")||(class2.value==="Sorcerer")||(class2.value==="Wizard")) {var b = level2;}
    else if ((class2.value==="Paladin")||(class2.value==="Ranger")) {var b = Math.floor(level2 / 2);}
    else if (class2.value==="Artificer") {var b = Math.ceil(level2 / 2);}
    else if ((class2.value==="Fighter")||(class2.value==="Rogue")) {var b = Math.floor(level2 / 3);}
    else {var b = 0;}
    var spellCasterLevel = a + b;
    event.value = spellCasterLevel;
    }
else if ((class1.value==="Fighter")||(class1.value==="Rogue"))
    {
    var a = Math.floor(level1 / 3);
    if ((class2.value==="Bard")||(class2.value==="Cleric")||(class2.value==="Druid")||(class2.value==="Sorcerer")||(class2.value==="Wizard")) {var b = level2;}
    else if ((class2.value==="Paladin")||(class2.value==="Ranger")) {var b = Math.floor(level2 / 2);}
    else if (class2.value==="Artificer") {var b = Math.ceil(level2 / 2);}
    else if ((class2.value==="Fighter")||(class2.value==="Rogue")) {var b = Math.floor(level2 / 3);}
    else {var b = 0;}
    var spellCasterLevel = a + b;
    event.value = spellCasterLevel;
    }
else
    {
    var spellCasterLevel = 0;
    event.value = spellCasterLevel;
    }

But when I change the value of the first dropdown field (Class1) I get either "NaN", "[object Field]0", or "0". The last one is only if I leave the field empty.

 

What is going wrong?

TOPICS
Create PDFs , General troubleshooting , JavaScript

Views

32

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 25, 2022 Aug 25, 2022

level1 and level2 are field objects.

To use the values of the fields you must use e.g. level1.value

Likes

Translate

Translate
Community Expert ,
Aug 25, 2022 Aug 25, 2022

Copy link to clipboard

Copied

level1 and level2 are field objects.

To use the values of the fields you must use e.g. level1.value

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 25, 2022 Aug 25, 2022

Copy link to clipboard

Copied

No, "level1" and "level2" are variables defined at the start of the code. "Level1" and "Level2" are the fields.

And since I havn't implemented the levels in the code yet it can't be a issue. The only fields that are used in the code are "Class1" and "Class2" and the variables "class1" and "class2".

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 25, 2022 Aug 25, 2022

Copy link to clipboard

Copied

And the way I'm defining the variables "class1", "class2", "level1", and "level2" is used in many other places in the document and it works perfectly.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 25, 2022 Aug 25, 2022

Copy link to clipboard

Copied

Sorry I'm tired... I am using the "level1" and "level2" variables in the code 😛 But I still don't think that they are the problem since they are working in other places.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 25, 2022 Aug 25, 2022

Copy link to clipboard

Copied

LATEST

No, it's not. You're using them incorrectly. For example, here:

Math.floor(level2 / 2);

It should be:

Math.floor(level2.value / 2);

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines