These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

if else statement/modulus operator issue

xiutwo
Registered: May 10 2009
Posts: 4
Answered

hello, i'm having a problem with a mildly complex calculation. what i want to have happen is this:

if Acrobatics3 is less than or equal to 4, skillCost equals 1. if Acrobatics3 is greater than 4 and less than or equal to 8, skillCost equals 2. (and so on, with skillCost going up 1 point for every 4 points in Acrobatics3).

the code i've been trying to get to work is the following:

var acr = this.getField("Acrobatics3");
var skc = acr / 4;

if (acr % 4 == 0) event.value = skc;
else event.value = skc + 1;

i have to note that my understanding of javascript is fairly limited, and i feel like i'm missing something glaringly obvious. i was told by a few people that using the modulus operator would solve my problem, and i'm pretty sure i have the logic right. the error i get is that the code doesn't match the format of the field - but the text fields involved are formatted as numbers. when i tried using Math.floor, the error was gone but the code still didn't work. the code is in the custom calculation field of skillCost. what am i doing wrong?

thank you ahead of time.

My Product Information:
Acrobat Pro 9.0, Macintosh
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You need to access the "value" of the field object not just the field object. Just like you accessed the 'value' of the 'event' object.
var acr = this.getField("Acrobatics3").value;var skc = acr / 4;if (acr % 4 == 0) event.value = skc;else event.value = skc + 1;

George Kaiser

xiutwo
Registered: May 10 2009
Posts: 4
ah, i knew it was something glaringly obvious. thank you for that, although now i'm getting another error. if i put in 4 or a multiple thereof in the Acrobatics3 field, nothing happens but i get no error. if i put in anything else (like say, 3, or 15), i get an error saying the value entered doesn't match the format of the field skillCost.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
What is the "Format" of the field 'skillCost'?

Are there anyother calculations that 'Acrobatics3' depends upon?

You can try a format of 'None' and see what results you obtain for different values.

You can replace the code with and see the values that are being used and generted.

var acr = this.getField("Acrobatics3").value;var skc = acr / 4;console.show();console.clear();console.println('acr = ' + acr);console.println('skc = ' + skc);console.println('acr % 4 = ' + (acr % 4) );if (acr % 4 == 0){event.value = skc;console.println('skc')} else {event.value = skc + 1;console.println('skc + 1')}

George Kaiser

xiutwo
Registered: May 10 2009
Posts: 4
thank you! i got it to work finally :) being able to see what it was doing helped a huge amount. i ended up using this:

var acr = this.getField("Acrobatics3").value;
var skc = acr / 4;
console.show();
console.clear();
console.println('acr = ' + acr);
console.println('skc = ' + skc);
console.println('acr % 4 = ' + (acr % 4) );
if (acr % 4 == 0){
event.value = Math.floor(skc);
console.println('skc' + skc)
} else {
event.value = Math.floor(skc + 1);
console.println('skc + 1' + skc + 1)
}

which returned exactly the results i wanted. formatting skillCost to none (it was formatted to number previously) and adding the Math.floor function is what did it in the end.

again, thank you :)