4 Replies Latest reply on Aug 11, 2014 1:16 AM by try67

    For loop not running?


      I was attempting a simple for loop in one of my custom calculations in a form and couldn't figure out why it wasn't working. So I tried to make the simplest of simple for loops and it just returns 1. Unless I'm wrong, wouldn't this be 10?


      var n = 0

      for ( i = 1; i <= 10; i++ ); {

      var n = n + 1;


      event.value = n

        • 1. Re: For loop not running?
          MichaelN Level 3

          There is a typo in line 2 of your code.


          It should be:


          for ( i = 1; i <= 10; i++ ) {

          • 2. Re: For loop not running?
            Kerberos Level 1

            I did it, but now the output is 0. I don't know much about java so this is baffling me. I can get it to work in Lua. But for some reason I can't wrap my head around java.

            • 3. Re: For loop not running?
              George_Johnson MVP & Adobe Community Professional

              Besides the error noted by Michael, this is more standard JavaScript:


              var n = 0;

              for (var i = 1; i <= 10; i++) {

                  n +=  1;


              event.value = n;

              • 4. Re: For loop not running?
                try67 MVP & Adobe Community Professional

                Just so you understand your error, the issue was that you were re-defining the n variable in each iteration of the loop by using the "var" keyword before it.

                So each instance of the variable existed and maintained separately.

                When the "var" keyword was removed (inside the loop), it caused JS to look for that variable in the higher code scope, and then it found the one defined in the first line, and used it.


                JavaScript is very lenient about doing this kind of thing, but with almost any other programming language doing what you did would yield a compilation error.