Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Struggling with nested if statement

New Here ,
Feb 25, 2020 Feb 25, 2020

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.

TOPICS
Acrobat SDK and JavaScript
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Feb 26, 2020 Feb 26, 2020

You need to study the core JS-syntax first.

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

Translate
Community Expert , Feb 28, 2020 Feb 28, 2020

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
...
Translate
Community Expert ,
Feb 25, 2020 Feb 25, 2020

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 26, 2020 Feb 26, 2020

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);

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

You need to study the core JS-syntax first.

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 26, 2020 Feb 26, 2020

(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

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

You can use:

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 26, 2020 Feb 26, 2020

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);

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

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

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

Why does you use if { ?

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

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/

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 26, 2020 Feb 26, 2020

 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 28, 2020 Feb 28, 2020
LATEST

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);
}

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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