By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

Switch Within A Switch

P: n/a
Hello,

I am working on an Adobe Acrobat file that uses javascript for
calculations. I am trying to create a field that uses two other fields
to determine a number value. I think this requires a switch within a
switch, but I am not sure how to do this, as I am a beginner when it
comes to javascript. In esscense, I want to set up a code so that each
case of value A is calculated at each case of value B.

Example:
There are 9 cases for value A.
There are 30 cases for value B.
I want to produce a number at each value B for each case in value A.

I hope I have explained what I want to do well enough. If anyone could
help me set up the code for a scenario like this, I would greatly
appreciate it!

Thank you,
cytec123187

Apr 22 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
cy*********@gmail.com wrote:
I am working on an Adobe Acrobat file that uses javascript for
calculations. I am trying to create a field that uses two other fields
to determine a number value. I think this requires a switch within a
switch, but I am not sure how to do this, as I am a beginner when it
comes to javascript. In esscense, I want to set up a code so that each
case of value A is calculated at each case of value B.

Example:
There are 9 cases for value A.
There are 30 cases for value B.
I want to produce a number at each value B for each case in value A.


More details are needed. Which number? For what purpose?
PointedEars
Apr 22 '06 #2

P: n/a
Ok, I am making a character sheet for a roleplaying game. The 9 cases
of value A are character classes (Fringer, Noble, Scoundrel, Scout,
Soldier, Tech Specialist, Soldier, Force Adept, Jedi Consular, Jedi
Guardian), while the 30 cases of value B are character levels (1, 2,
3,...28, 29, 30). I am trying to create a field that will display a
bonus number based on the level and class of the character. For
instance, the bonus number for a level 1 Fringer is "+2" and the bonus
number for a level 4 Soldier is "+4." The number depends on both the
class and character level, which is why I think a switch within a
switch is needed (if possible). I am open to any method in order to
achieve the desired results. If you could think of a way I could go
about this, please let me know.

Apr 22 '06 #3

P: n/a

cy*********@gmail.com wrote:
Ok, I am making a character sheet for a roleplaying game. The 9 cases
of value A are character classes (Fringer, Noble, Scoundrel, Scout,
Soldier, Tech Specialist, Soldier, Force Adept, Jedi Consular, Jedi
Guardian), while the 30 cases of value B are character levels (1, 2,
3,...28, 29, 30). I am trying to create a field that will display a
bonus number based on the level and class of the character. For
instance, the bonus number for a level 1 Fringer is "+2" and the bonus
number for a level 4 Soldier is "+4."


If the numbers aren't variable (aside from class and level) you could
just build a data structure that contained everything and then do a
look up based on that input.

so data set up:

var aCharSheet = new Array();

aCharSheet["Fringer"][1] = "bonus for fringer level 1";
aCharSheet["Fringer"][2] = "bonus for fringer level 1";
aCharSheet["Fringer"][3] = "bonus for fringer level 1";
..
..
..
aCharSheet["Fringer"][30] = "bonus for fringer level 30";
aCharSheet["Noble"][1] = "etc";
then your fields could output a string for class (which you could
eaisly verify with regex to be the correct string type), and a number
for level:

var sClass = field1Output;
var fLevel = field2Output;

and then you would just do a look up into your array with those:

var sBonus = aCharSheet[sClass][fLevel];

If you could come up with a way for logic to generate the data that
would be cleaner, but nothing is wrong with brute force if it works for
you.

Apr 22 '06 #4

P: n/a
Jim
Cytec,
Adobe has its own unique javascript style and syntax which is
similar-to, but unlike the javascript used for web browsers. (different
methods, functions, etc.).

It has javascript listed in the main help index and it also has a
stand-alone javascript manual as well.

Your calculations can be done routinely by the acrobat forms window
which appears when you create a form field. Navigate to the 'FORMAT'
tab of that window and select the NUMBER format. Then navigate to the
CALCULATE tab and click on the "VALUE IS TH E SUM OF THE FOLLOWING
FIELDS" Acrobat has a selection box to click and select those (named)
form fields you already created with the numbers in them that you wish
to add together. On that same tab is a custom calculation area where
you can create your own script to calculate values. Take a peek at the
help index or javascript guide and you'll find all your answers! -Jim

Apr 22 '06 #5

P: n/a
cy*********@gmail.com wrote:
Ok, I am making a character sheet for a roleplaying game. The 9 cases
of value A are character classes (Fringer, Noble, Scoundrel, Scout,
Soldier, Tech Specialist, Soldier, Force Adept, Jedi Consular, Jedi
Guardian), while the 30 cases of value B are character levels (1, 2,
3,...28, 29, 30). I am trying to create a field that will display a
bonus number based on the level and class of the character. For
instance, the bonus number for a level 1 Fringer is "+2" and the bonus
number for a level 4 Soldier is "+4."


var characterSheet = {
fringer: [2, ...],
noble: [...],
scoundrel: [...],
scout: [...],
soldier: [1, 2, 3, 4, ...],
...
};

That is for zero-based character levels (level 1 soldier bonus is accessible
with characterSheet.soldier[0]). If you want to refer to character levels
by their real number, add a comma to the front of the element list to
define an element with index 0 and value `undefined'.

This literal object notation is supported by JavaScript 1.3+ (NN 4.0+),
JScript 3.0+ (IE 4.0+), and ECMAScript Ed. 3 implementations (since
December 1999). If you want to support other implementations, you have
to write

var characterSheet = new Object();
characterSheet.fringer = new Array(2, ...);
characterSheet.noble = new Array(...);
characterSheet.scoundrel = new Array(...);
characterSheet.scout = new Array(...);
characterSheet.soldier = new Array(1, 2, 3, 4, ...);
...

(and for character levels to be referred starting with 1:

characterSheet.fringer = new Array(anything, 2, ...);

It is unnecessary (and potentially error-prone, since it inherits more
properties and requires more memory) to make _characterSheet_ a reference
to an Array object (as mallen suggested), because no feature of Array
objects is used here so far (one could use an Array object if one wanted to
give each character class an index, but it would have to be numeric then
for a change of the Array object's `length' property).

There are no built-in associative arrays in JavaScript 1.0 and ECMAScript
implementations, what is addressed are only object properties, in dot
property accessor notation (here) or in bracket property accessor notation
(in mallen's approach).

Please quote what you are replying to next time:

<URL:http://jibbering.com/faq/faq_notes/pots1.html>
<URL:http://www.safalra.com/special/googlegroupsreply/>
HTH

PointedEars
Apr 22 '06 #6

P: n/a
PointedEars wrote:
var characterSheet = {
fringer: [2, ...],
noble: [...],
scoundrel: [...],
scout: [...],
soldier: [1, 2, 3, 4, ...],
...
}; That is for zero-based character levels (level 1 soldier bonus is accessible
with characterSheet.soldier[0]). If you want to refer to character levels
by their real number, add a comma to the front of the element list to
define an element with index 0 and value `undefined'.


I am not sure I understand what you are saying. Please note that this
is within an Acrobat document. The values are entered earlier in the
document and I want Acrobat to compute, using a JavaScript code, a
value when the Class field is X and the Character_Level field is Y. I
thought about using logic statements to arrange this, but I don't know
how. For example...

var a = this.getField("Class");
var b = this.getField("Character_Level");
switch(a.value)
{
case "Fringer":
if (b.value == 1)
event.value = "+2";

....but I don't want an if/else because I am limited to only two option
when there are 30 options I need to include. Is there a way to open a
switch within a switch, something like this...

var a = this.getField("Class");
var b = this.getField("Character_Level");
switch(a.value)
{
case "Fringer":
switch(b.value)
{
case 1:
event.value = "+2";

I don't know how to do advanced scripting. I am an unskilled novice at
best. The following is a portion of the data I need entered. Maybe it
will help in some way...

The two fields I wish to use to determine a number from are not all
numbers themselves. The first field is a group of nine labels. The
second field is an entry point for a level number, 1-30. Each label
from the first field has a different bonus number at each level from
the second field. I need to be able to output a specific number into a
third field at each label name and level number from the first two
fields.

Example
CLASS CHARACTER_LEVEL THIRD (OUTPUT) FIELD
Fringer 1 +2
2 +3
3 +3
4 +4
5 +4
6 +5
7 +5
8 +6
9 +6
10 +7
11 +7
12 +8
13 +8
14 +9
15 +9
16 +10
17 +10
18 +11
19 +11
20 +12
21 +12
22 +13
23 +13
24 +14
25 +14
26 +15
27 +15
28 +16
29 +16
30 +17
Noble 1 +0
2 +0
3 +1
4 +1
5 +1
6 +2
7 +2
8 +2
9 +3
10 +3
11 +3
12 +4
13 +4
14 +4
15 +5
16 +5
17 +5
18 +6
19 +6
20 +6
21 +7
22 +7
23 +7
24 +8
25 +8
26 +8
27 +9
28 +9
29 +9
30 +10
The list continues on for each of the seven remaining labels of the
first field. Notice the output values change for each level AND each
label. I have gone through the JavaScript guides in Acrobat, and I am
unsure how to do the above. If you could help me, please let me know.

Thanks,
cytec123187

Apr 22 '06 #7

P: n/a
cy*********@gmail.com writes:
I am not sure I understand what you are saying. Please note that this
is within an Acrobat document.
(I don't know which features of modern Javascript the scripting engine
of pdf-viewers implement, but the functionality should be available,
even though some shorthand notations might not be.)
I thought about using logic statements to arrange this, but I don't
know how. For example...
The point here is that you have a table of data to implement.
Implementing that with switches and if statments is overkill, and you
should instead make a look-up table in your code and use generic code
to look up the values.
var a = this.getField("Class");
var b = this.getField("Character_Level");
switch(a.value)
{
case "Fringer":
if (b.value == 1)
event.value = "+2";
If you just had a table, you could look up the value as:

var className = a.value;
var level = b.value;
event.value = lookupTable[className][level - 1];

This could be the table Thomas Lahn suggested:

var lookupTable = {
Fringer: [2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12, (etc)],
Noble: [0,0,1,1,1,2,2,2,3,3, (etc)],
...etc.
};

(or, if object and/or array literal notation is not available,
var lookuptTable = new Object();
lookupTable["Fringer"] = new Array(2,3,3,4,4,5,5, ...);
lookupTable["Noble"] = new Array(0,0,1,1,1,2,2,2,...);
...
)
...but I don't want an if/else because I am limited to only two option
when there are 30 options I need to include. Is there a way to open a
switch within a switch, something like this...

var a = this.getField("Class");
var b = this.getField("Character_Level");
switch(a.value)
{
case "Fringer":
switch(b.value)
{
case 1:
event.value = "+2";
Have you tried it?
It should work. It's still *far* from the simplest solution.

I don't know how to do advanced scripting. I am an unskilled novice at
best. The following is a portion of the data I need entered. Maybe it
will help in some way...

Another solution suggests itself, when you notice the regularity of
the numbers. You can compute the bonus value from the level through a
mathematical formula, and implement that as a function.

function goodSave(level) {
return 2 + Math.floor(level / 2);
}
function badSave(level) {
return Math.floor(level / 3);
}

Then you can make a look-up table from class name to bonus *function*:

var lookupTable = {
Fringer : goodSave,
Noble : badSave,
... etc...
}

and then look up the function and use it:

var className = this.getField("Class");
var level = Number(this.getField("Character_Level"));

event.value = lookupTable[className](level);

Again, I have no experience with Acrobat, or its Javascript support,
so some of this might not work directly.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 22 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.