6 Replies Latest reply on Oct 10, 2013 6:20 AM by James A Hiltz CET

    else if statement problem is originating in the condition

    James A Hiltz CET Level 1

      I need assistance with the following else if statement. The problem is originating in the condition part of the statement.

       

      // Get the field values, as numbers

      var kn2 = getField("Kn_2").value;

      var dia2 = +getField("Dia_2").value;

      var L2 = +getField("Length_2").value;

       

      if (dia2 === 0) {

      1. event.value = "";

      }

      else if ((L2/dia2) > 1.87) {

      1. event.value = (kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2)));

      }

      else if (L2/dia2 <= 1.75) {

      1. event.value = (kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2)))*0.98;

      }

      else if (L2/dia2 <= 1.50) {

      1. event.value = (kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2)))*0.96;

      }

      else if (L2/dia2 <= 1.25) {

      1. event.value = (kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2)))*0.93;

      }

      else if (L2/dia2 <= 1.00) {

      1. event.value = (kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2)))*0.87;

      }

        • 1. Re: else if statement problem is originating in the condition
          try67 MVP & Adobe Community Professional

          Why aren't you converting the first variable to a number as well (using the + operator), like the other two?

          And what are those "a. "-texts at the start of each line?

           

          The problem is not very clear. Please explain what exactly is going wrong with the code, and provide any error messages you're getting.

          • 2. Re: else if statement problem is originating in the condition
            MarkWalsh Level 4

            I think the 'a.' are a html list item, not a part of the original code.

             

            As for the code itself, the order is incorrect - the last 3 'else if' statements will never run because if the value is <= 1.25, then it is also <= 1.75. You need to reverse the order of the last 4 statements. Other than that, I do not know what problem you are encountering.

             

            And, what is supposed to happen if the value is > 1.75 and <= 1.87?

            • 3. Re: else if statement problem is originating in the condition
              James A Hiltz CET Level 1

              Than you for your reply.

              I copyed and pasted from a seprate program and for some reason the "a" just apeared.

              in my script the "a " does not exist.

              the reason why I can not just calculate the variables is the siply the variables are truly variables.

              the input for both fields are independent and always changing.

               

              If the value is >1.75  the number is not corrected.

              • 4. Re: else if statement problem is originating in the condition
                James A Hiltz CET Level 1

                Ok so changed the order and insured the "a" is removed but the calculation just simply will not go to the next condition.

                 

                // Get the field values, as numbers

                var kn2 = getField("Kn_2").value;

                var dia2 = +getField("Dia_2").value;

                var L2 = +getField("Length_2").value;

                 

                 

                if (dia2 === 0) {

                event.value = "";

                }

                else if (L2/dia2 <= 1.00) {

                event.value = ((kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2))))*0.87;

                }

                else if (L2/dia2 <= 1.25) {

                event.value = ((kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2))))*0.93;

                }

                else if (L2/dia2 <= 1.50) {

                event.value = ((kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2))))*0.96;

                }

                else if (L2/dia2 <= 1.75) {

                event.value = ((kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2))))*0.98;

                }

                else if ((L2/dia2) > 1.75) {

                event.value = (kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2)));

                }

                • 5. Re: else if statement problem is originating in the condition
                  gkaiseril MVP & Adobe Community Professional

                  Nested if statements can be confusing and difficult to code.

                   

                  Have you tried adding "console.println" statements to see what values are being processed and where they are being processed?

                   

                  // Get the field values, as numbers

                  var kn2 = getField("Kn_2").value;

                  var dia2 = +getField("Dia_2").value;

                  var L2 = +getField("Length_2").value;

                  console.show();

                  console.clear();

                  console.println("kn2 = " + kn2);

                  console.println("dia2 = " + dia2);

                  console.println("L2 = " + L2);

                   

                  if (dia2 == 0) {

                  console.println("zero divisor");

                  event.value = "";

                  } else if (L2/dia2 <= 1.00) {

                  console.println( "L2/dia2 <= 1.00 = " + (L2/dia2));

                  event.value = ((kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2))))*0.87;

                  } else if (L2/dia2 <= 1.25) {

                  console.println( "L2/dia2 <= 1.25 = " + (L2/dia2));

                  event.value = ((kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2))))*0.93;

                  } else if (L2/dia2 <= 1.50) {

                  console.println( "L2/dia2 <= 1.50 = " + (L2/dia2));

                  event.value = ((kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2))))*0.96;

                  } else if (L2/dia2 <= 1.75) {

                  console.println( "L2/dia2 <= 1.75 = " + (L2/dia2));

                  event.value = ((kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2))))*0.98;

                  } else if ((L2/dia2) > 1.75) {

                  console.println( "L2/dia2 > 1.75 = " + (L2/dia2));

                  event.value = (kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2)));

                  }

                   

                  Have you considered using a complex logical statement to test for 2 or more conditions?

                   

                  // Get the field values, as numbers
                  var kn2 = getField("Kn_2").value;
                  var dia2 = +getField("Dia_2").value;
                  var L2 = +getField("Length_2").value;

                  event.value = "";
                  if (dia2 > 0 && (L2/dia2 <= 1) ) {
                  event.value = ((kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2))))*0.87;
                  } else if (dia2 > 0 && (L2/dia2 > 1) && (L2/dia2 <= 1.25) ) {
                  event.value = ((kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2))))*0.93;
                  } else if (dia2 > 0 && (L2/dia2 > 1.25) && (L2/dia2 <= 1.50) ) {
                  event.value = ((kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2))))*0.96;
                  } else if (dia2 > 0 && (L2/dia2 > 1.5) && (L2/dia2 <= 1.75) ) {
                  event.value = ((kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2))))*0.98;
                  } else if (dia2 > 0 && ( (L2/dia2) > 1.75 ) ) {
                  event.value = (kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2)));
                  }


                  Have you considered using the "switch" statement?

                   

                  // Get the field values, as numbers

                  var kn2 = getField("Kn_2").value;

                  var dia2 = +getField("Dia_2").value;

                  var L2 = +getField("Length_2").value;

                   

                  switch (true) {

                  case (dia2 > 0 && (L2/dia2 <= 1.00) ) :

                  event.value = ((kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2))))*0.87;

                  break;

                  case (dia2 > 0 && (L2/dia2 > 1.00) && (L2/dia2 <= 1.25) ) :

                  event.value = ((kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2))))*0.93;

                  break;

                  case (dia2 > 0 && (L2/dia2 > 1.25) && (L2/dia2 <= 1.50) ) :

                  event.value = ((kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2))))*0.96;

                  break;

                  case (dia2 > 0 && (L2/dia2 > 1.50) && (L2/dia2 <= 1.75) ) :

                  event.value = ((kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2))))*0.98;

                  break;

                  case (dia2 > 0 && ( (L2/dia2) > 1.75 ) ) :

                  event.value = (kn2 * 1000) / (Math.PI * (Math.pow((dia2 / 2),2)));

                  break;

                  default:

                  event.value = "";

                  break;

                  }

                  • 6. Re: else if statement problem is originating in the condition
                    James A Hiltz CET Level 1

                    Alison,

                     

                    I used the console to point out where my problem was.

                     

                    Thank you for your assistance.

                    It is people like you who take the time from your day to assist others that makes the world a better place.

                     

                    Cheers,