473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Switch Within A Switch

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
7 1978
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
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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: william c | last post by:
I'm trying to use here docs within a switch to output html. The code below works (i.e. prints a Foo1 headline below my tab graphic) but if I take out the comments and use the entire switch...
15
by: Mike and Jo | last post by:
I've been converting some code to C++. I'm trying to use the Switch function to compare a result. Is it possible to use switch to evaluate '>0', '<0', 0? Example switch (result) { case...
35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
17
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can...
13
by: webzila | last post by:
Hello, I have to write a program for an 8051 micro-controller using micro-C to monitor Switch 1 and if the switch in pushed the message "switch 1 pushed" should be displayed in the LCD. Also the...
5
by: Nadav | last post by:
Hi, I am using FileStream's Async API: BeginRead/EndRead, upon completion callback execution I use the read data and call EndRead, Taking that in mind, I Wonder... does calling EndRead will cause...
13
by: PeterZ | last post by:
Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch...
25
by: v4vijayakumar | last post by:
'continue' within switch actually associated with the outer 'while' loop. Is this behavior protable? int ch = '\n'; while (true) { switch(ch) { case '\n': cout << "test"; continue; } }
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.