Else If Statement hierarchy
Copy link to clipboard
Copied
I have an else if statement that is completely finished, however it seems that the hierarchy is off and I don't know what order it should be in.
Can anyone help with this? reading and writing test fields are number based on values from 0-120, any field marked with a Q is a checkbox field. I believe I have covered all possible situations in the code so nothing with that needs to be changed, only the order of statements so that they all work properly.
//
var f = this.getField("Reading Test");
var g = this.getField("Writing Test");
var h = this.getField("ReadingQ");
var i = this.getField("WritingQ");
var j = this.getField("ESLQ");
var k = this.getField("EngQ");
if (k.valueAsString=="On") event.value = "";
else if ((f.valueAsString=="") && (g.valueAsString=="") && (j.valueAsString=="On"))event.value = "";
else if ((f.valueAsString=="") && (g.valueAsString=="") && (j.valueAsString=="Off"))event.value = "";
else if ((i.valueAsString=="On") && (h.valueAsString=="On"))event.value = "ENG 111 - Introduction to College Writing, 3 cr.";
else if ((i.valueAsString=="On") && (f.valueAsString<="054") && (j.valueAsString=="Off"))event.value = "ENG 4 - Developmental Reading, 0 cr./4 eq. cr.";
else if ((h.valueAsString=="On") && (g.valueAsString<="055") && (j.valueAsString=="Off"))event.value = "ENG 1 - Developmental Writing I, 0 cr./4 eq. cr.";
else if ((i.valueAsString=="On") && (f.valueAsString<="120") && (j.valueAsString=="Off"))event.value = "ENG 111 - Introduction to College Writing, 3 cr.";
else if ((h.valueAsString=="On") && (g.valueAsString<="120") && (j.valueAsString=="Off"))event.value = "ENG 111 - Introduction to College Writing, 3 cr.";
else if ((i.valueAsString=="On") && (f.valueAsString<="039") && (j.valueAsString=="On"))event.value = "ENG 9 - Basic Reading for Non-Native Speakers of English, 0 cr./4 eq. cr.";
else if ((i.valueAsString=="On") && (f.valueAsString<="047") && (j.valueAsString=="On"))event.value = "ENG 10 - Developmental Reading for Non-Native Speakers of English, 0 cr./4 eq. cr.";
else if ((i.valueAsString=="On") && (f.valueAsString<="054") && (j.valueAsString=="On"))event.value = "ENG 39 - Reading for Non-Native Speakers of English, 0 cr./4 eq. cr.";
else if ((i.valueAsString=="On") && (f.valueAsString<="120") && (j.valueAsString=="On"))event.value = "ENG 111 - Introduction to College Writing, 3 cr.";
else if ((h.valueAsString=="On") && (g.valueAsString<="039") && (j.valueAsString=="On"))event.value = "ENG 7 - Developmental English for Non-Native Speakers, 0 cr./4 eq. cr.";
else if ((h.valueAsString=="On") && (g.valueAsString<="047") && (j.valueAsString=="On"))event.value = "ENG 8 - Developmental Writing for Non-Native Speakers of English, 0 cr./4 eq. cr.";
else if ((h.valueAsString=="On") && (g.valueAsString<="055") && (j.valueAsString=="On"))event.value = "ENG 37 - Writing for Non-Native Speakers of English, 0 cr./4 eq. cr.";
else if ((h.valueAsString=="On") && (g.valueAsString<="120") && (j.valueAsString=="On"))event.value = "ENG 111 - Introduction to College Writing, 3 cr.";
else if ((g.valueAsString>="056") && (f.valueAsString<="054") && (j.valueAsString=="Off"))event.value = "ENG 4 - Developmental Reading, 0 cr./4 eq. cr.";
else if ((f.valueAsString>="055") && (g.valueAsString<="047") && (j.valueAsString=="Off"))event.value = "ENG 1 - Developmental Writing I, 0 cr./4 eq. cr.";
else if ((f.valueAsString<="054") && (g.valueAsString<="055") && (j.valueAsString=="Off"))event.value = "ENG 1";
else if ((f.valueAsString>="055") && (g.valueAsString<="055") && (j.valueAsString=="Off"))event.value = "ENG 1 - Developmental Writing I, 0 cr./4 eq. cr.";
else if ((f.valueAsString<="120") && (g.valueAsString<="120") && (j.valueAsString=="Off"))event.value = "ENG 111 - Introduction to College Writing, 3 cr.";
else if ((f.valueAsString<="039") && (g.valueAsString<="039") && (j.valueAsString=="On"))event.value = "ENG 7";
else if ((f.valueAsString<="039") && (g.valueAsString<="047") && (j.valueAsString=="On"))event.value = "ENG 8";
else if ((f.valueAsString<="039") && (g.valueAsString<="055") && (j.valueAsString=="On"))event.value = "ENG 37";
else if ((f.valueAsString<="039") && (g.valueAsString<="120") && (j.valueAsString=="On"))event.value = "ENG 9 - Basic Reading for Non-Native Speakers of English, 0 cr./4 eq. cr.";
else if ((f.valueAsString<="047") && (g.valueAsString<="039") && (j.valueAsString=="On"))event.value = "ENG 7";
else if ((f.valueAsString<="047") && (g.valueAsString<="047") && (j.valueAsString=="On"))event.value = "ENG 8";
else if ((f.valueAsString<="047") && (g.valueAsString<="055") && (j.valueAsString=="On"))event.value = "ENG 37";
else if ((f.valueAsString<="047") && (g.valueAsString<="120") && (j.valueAsString=="On"))event.value = "ENG 10 - Developmental Reading for Non-Native Speakers of English, 0 cr./4 eq. cr.";
else if ((f.valueAsString<="054") && (g.valueAsString<="039") && (j.valueAsString=="On"))event.value = "ENG 7";
else if ((f.valueAsString<="054") && (g.valueAsString<="047") && (j.valueAsString=="On"))event.value = "ENG 8";
else if ((f.valueAsString<="054") && (g.valueAsString<="055") && (j.valueAsString=="On"))event.value = "ENG 37";
else if ((f.valueAsString<="054") && (g.valueAsString<="120") && (j.valueAsString=="On"))event.value = "ENG 39 - Reading for Non-Native Speakers of English, 0 cr./4 eq. cr.";
else if ((f.valueAsString<="120") && (g.valueAsString<="039") && (j.valueAsString=="On"))event.value = "ENG 7 - Developmental English for Non-Native Speakers, 0 cr./4 eq. cr.";
else if ((f.valueAsString<="120") && (g.valueAsString<="047") && (j.valueAsString=="On"))event.value = "ENG 8 - Developmental Writing for Non-Native Speakers of English, 0 cr./4 eq. cr.";
else if ((f.valueAsString<="120") && (g.valueAsString<="055") && (j.valueAsString=="On"))event.value = "ENG 37 - Writing for Non-Native Speakers of English, 0 cr./4 eq. cr.";
else if ((f.valueAsString<="120") && (g.valueAsString<="120") && (j.valueAsString=="On"))event.value = "ENG 111 - Introduction to College Writing, 3 cr.";
else event.value = "";
Copy link to clipboard
Copied
There isn't a hierarchy, it just each one in turn. To debug this you need to do the same, running it (on paper, perhaps) with values until you find exactly which lines aren't suiting you.
We certainly can't help anyone fix code when they haven't told us EXACTLY what they want to do... often this is the point, as a programmer, where you really you don't know exactly what you want to do. I have to go away in a darkened room and work out my real objectives, and how EXACTLY to achieve them. Only then can I come back and fix the code: code is not a substitute for a really detailed knowledge of what you want to do.
Copy link to clipboard
Copied
The issue is not the hierarchy (which is just the order in which you write them), but statements like this:
if (f.valueAsString<="039" ..
The "<=" operator can only be used on numbers, and "039" is not a number, it's a string.
You need to change it to something like this:
if (Number(f.valueAsString)<=39 ...
Copy link to clipboard
Copied
Are you sure <= can only be used on numbers. JavaScript Comparison and Logical Operators suggests otherwise. It says that where one operand is a number, they are both treated as numbers, and when both are strings they are compared "alphabetically":
When comparing two strings, "2" will be greater than "12", because (alphabetically) 1 is less than 2.
If this is right, though, the test if (f.valueAsString<="039" ) is going to succeed when f is "038" and fail when it is "38", which might or might not be the aim.
Copy link to clipboard
Copied
Yes, technically you can use it on other variable types, but it's generally a bad idea (as the example shows), unless you know what you're doing and are doing so explicitly. I don't think that's what the OP intended to do when they used it.
Copy link to clipboard
Copied
I would look at using the switch operator and a logical expression for the case select statement.
Copy link to clipboard
Copied
I wouldn't use this method at all. Both "if" and "switch" statements are inefficiency and cumbersome for large decision trees like this. Sinc the conditions and results all follow the same pattern it would be much better to put them into an array or object, then write much smaller code to search the object.
Use the Acrobat JavaScript Reference early and often

