## Struggling with nested if statement

Feb 25, 2020

Copied

I need to create an if then statement ot check multiple variables.

I have a 100yr floodplain (100yr) A (500yr) a (HAG) a (LAG) and a (Zone)

I need to calculate min FF if  Zone = x-shaded min ff = HAG + 1 If LAG is below 500 yr & above 100 yr: Min FF must be at least = to 500 yr
If LAG is below 500 yr & 100 yr BFE: Min FF must be at least 2’ above 500

I started by creating varialbe for LAG HAG 100yr and 500yr but not sure how to check the zone and where to go from there.

var A = this.getField ("500yr").value;
var B = this.getField ("LAG").value;
var C = this.getField ("HAG").value;
Var D = this.getField ("100yr").value;
var E = C + 1;
if(B > A)

Not even sure if I am on the right track with this any help is appreciated.

Here's how the code is written with indents and brackets.

The brackest are only necessary when there is more than one line in the "if" block. But I've shown them here for completeness.

I've also added extra parenthese around some of the comparison opertators to enforce the grouping/order of operations. This is very helpful when operator precedence is not obvious.

``````var Z = this.getField ("Zone").valueAsString; // Value as a string
var A = this.getField ("500yr").value; // Value as a number
var B = this.getField ("LAG").value; // Value as a number
var C = this.getField ("HAG").value; // Value as a number
var D = this.getField ("100yr").value; // Value as a number

if (((Z == "X-SHADED") || (Z == "AE") || (Z == "X-SHADED/AE")) && (B < A) && (B < D))
{
event.value = (A + 2);
}
else if (((Z == "X-SHADED") || (Z == "AE")) && (B > D) && (B < A))
{
event.value = (A);
}
else if (Z == "X")
{
event.value = (C + 1);
}``````

TOPICS
Acrobat SDK and JavaScript, How to, PDF forms

Views

155

Likes

Report

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

## Struggling with nested if statement

Feb 25, 2020

Copied

I need to create an if then statement ot check multiple variables.

I have a 100yr floodplain (100yr) A (500yr) a (HAG) a (LAG) and a (Zone)

I need to calculate min FF if  Zone = x-shaded min ff = HAG + 1 If LAG is below 500 yr & above 100 yr: Min FF must be at least = to 500 yr
If LAG is below 500 yr & 100 yr BFE: Min FF must be at least 2’ above 500

I started by creating varialbe for LAG HAG 100yr and 500yr but not sure how to check the zone and where to go from there.

var A = this.getField ("500yr").value;
var B = this.getField ("LAG").value;
var C = this.getField ("HAG").value;
Var D = this.getField ("100yr").value;
var E = C + 1;
if(B > A)

Not even sure if I am on the right track with this any help is appreciated.

Here's how the code is written with indents and brackets.

The brackest are only necessary when there is more than one line in the "if" block. But I've shown them here for completeness.

I've also added extra parenthese around some of the comparison opertators to enforce the grouping/order of operations. This is very helpful when operator precedence is not obvious.

``````var Z = this.getField ("Zone").valueAsString; // Value as a string
var A = this.getField ("500yr").value; // Value as a number
var B = this.getField ("LAG").value; // Value as a number
var C = this.getField ("HAG").value; // Value as a number
var D = this.getField ("100yr").value; // Value as a number

if (((Z == "X-SHADED") || (Z == "AE") || (Z == "X-SHADED/AE")) && (B < A) && (B < D))
{
event.value = (A + 2);
}
else if (((Z == "X-SHADED") || (Z == "AE")) && (B > D) && (B < A))
{
event.value = (A);
}
else if (Z == "X")
{
event.value = (C + 1);
}``````

TOPICS
Acrobat SDK and JavaScript, How to, PDF forms

Views

156

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 25, 2020 0
11 Replies 11
Feb 25, 2020

Copied

So this is a validation script?

What you need is not nested if-statements, necessarily, but to use logical operators, such as AND (&&) and OR (||) in your if-conditions.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 25, 2020 0
Feb 26, 2020

Copied

Yes I'm just not sure I'm on the right track. I need to check min FF against zone if it is anything other than x or x-shaded which is a text value than min FF = HAG + 1 then it will check that the LAG is higher or lower than the 100yr and 500yr if it is lower then the value will be equal to 500 yr  + 2 if it is Higher than min FF = 500yr.

here is waht I am thinking

var A = this.getField ("500yr").value;
var B = this.getField ("LAG").value;
var C = this.getField ("HAG").value;
Var D = this.getField ("100yr").value;
var E = C + 1;
if(B < A) && (B < D);
E = (A + 2);

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 26, 2020 0
Feb 26, 2020

Copied

You need to study the core JS-syntax first.

This site offers a good introduction: https://www.w3schools.com/js/default.asp

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 26, 2020 0
Feb 26, 2020

Copied

(function(){
// Get the feild values
var Z = this.getField ("Zone").valueAsString; // Value as a string
var A = this.getField ("500yr").value; // Value as a number
var B = this.getField ("LAG").value; // Value as a number
var C = this.getField ("HAG").value; // Vlaue as a number
Var D = this.getField ("100yr").value; // Value as a number

if (Z = "X-SHADED" OR "AE" OR "X-SHADED/AE" && B < A && B < D){
event.value = (A + 2);
}else if{ (Z = "X-SHADED" OR "AE" && B > D && B < A){
event.value = (A);
}else if{ (Z = "X")
event.value = (c + 1);
}
})();

I think I am gettign closer but still not quite there

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 26, 2020 0
Feb 26, 2020

Copied

You can use:

if ((Z == "X-SHADED" || Z == "AE") && B > D && B < A) {

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 26, 2020 0
Feb 26, 2020

Copied

Here is what I have so far

(function(){
// Get the field values
var Z = this.getField ("Zone").valueAsString; // Value as a string
var A = this.getField ("500yr").value; // Value as a number
var B = this.getField ("LAG").value; // Value as a number
var C = this.getField ("HAG").value; // Vlaue as a number
var D = this.getField ("100yr").value; // Value as a number

if ((Z == "X-SHADED" || Z == "AE" || Z == "X-SHADED/AE") && B < A && B < D){
event.value = (A + 2);
}else if{ ((Z = "X-SHADED" || Z == "AE") && B > D && B < A){
event.value = (A);
}else if{ (Z = "X")
event.value = (c + 1);
}
}})();

debugger is saying that I am missing an ( condition on line 12 which is event.value = (A);

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 26, 2020 0
Feb 26, 2020

Copied

No, it's the line before that. if must be followed by round parentheses, not curly ones.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 26, 2020 0
Feb 26, 2020

Copied

Why does you use if { ?

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 26, 2020 0
Feb 26, 2020

Copied

First, you need to get the basic "if" syntax correct.

Its also very helpful to write the code with indents to show the nesting in a way that is easy to understand.

Here's an article on using the "if" statement.

https://www.acrobatusers.com/tutorials/conditional-execution/

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 26, 2020 0
Feb 26, 2020

Copied

Here is what I have now,

// Get the field values
var Z = this.getField ("Zone").valueAsString; // Value as a string
var A = this.getField ("500yr").value; // Value as a number
var B = this.getField ("LAG").value; // Value as a number
var C = this.getField ("HAG").value; // Value as a number
var D = this.getField ("100yr").value; // Value as a number

if ((Z == "X-SHADED" || Z == "AE" || Z == "X-SHADED/AE") && B < A && B < D)
event.value = (A + 2);

else if ((Z == "X-SHADED" || Z == "AE") && B > D && B < A)
event.value = (A);

else if (Z == "X")
event.value = (C + 1);

So far it appears to be working but I haven't tested it with all the necessary values yet. Thank you for helping me get this far

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 26, 2020 0
Feb 28, 2020

Copied

Here's how the code is written with indents and brackets.

The brackest are only necessary when there is more than one line in the "if" block. But I've shown them here for completeness.

I've also added extra parenthese around some of the comparison opertators to enforce the grouping/order of operations. This is very helpful when operator precedence is not obvious.

``````var Z = this.getField ("Zone").valueAsString; // Value as a string
var A = this.getField ("500yr").value; // Value as a number
var B = this.getField ("LAG").value; // Value as a number
var C = this.getField ("HAG").value; // Value as a number
var D = this.getField ("100yr").value; // Value as a number

if (((Z == "X-SHADED") || (Z == "AE") || (Z == "X-SHADED/AE")) && (B < A) && (B < D))
{
event.value = (A + 2);
}
else if (((Z == "X-SHADED") || (Z == "AE")) && (B > D) && (B < A))
{
event.value = (A);
}
else if (Z == "X")
{
event.value = (C + 1);
}``````

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 28, 2020 0