7 Replies Latest reply on Jun 5, 2016 4:47 PM by maxwyss

    how to limit a calculation and have remainder in new field

    pdfUser1 Level 1

      how can I limit the total to 40. Balance to populate into remainder field. This is the script I have

       

      var f = this.getField("Text13");

      var g = this.getField("Text14");

      var j = this.getField("Text15");

      event.value = f.value + g.value + j.value;

        • 1. Re: how to limit a calculation and have remainder in new field
          maxwyss Level 4

          Assuming that the total can not get beyond 79, you could do this:

           

          • Create a field for the remainder ("remainder")

           

          • Create a hidden, read-only field as carrier of the calculation ("calc").

           

          • Rename (if needed) the field for the result to "total"

           

          • Add this script to Calculation event of the calc field:

           

          var f = this.getField("Text13") ;

          var g = this.getField("Text14") ;

          var j = this.getField("Text15") ;

          var sum = f.value*1 + g.value*1 + h.value*1 ;

          this.getField("total").value = 40 * Math.floor(sum / 40) ;

          this.getField("remainder").value = sum % 40 ;

           

          And that should do it.

           

          It might be recommended to use more reasonable field names for the summands, however.

           

          Hope this can help.

          • 2. Re: how to limit a calculation and have remainder in new field
            pdfUser1 Level 1

            Hello Max, thanks for the quick reply. The script you provided worked, (as you said up to 79). But, as you know with form creating and javascripting fields, things change constantly. And, in my case I've decided to change the fields to only one field. And, have the "total" field max out at 8 then carry the balance over to the "remainder" field. I tried to work with your script and modify it to 8 by changing 40 to 8 and I wasn't successful. Can you guide me as to what to change and get your script to work with my changes?

             

            This is how I changed the fields:

            var f = this.getField("Text14") ;

             

            var sum = f.value*1 ;

            this.getField("total").value = 8 * Math.floor(sum / 8) ;

            this.getField("remainder").value = sum % 8 ;

             

            This kinda works up to 16. If I enter 17 into Text14 then 16 goes into the "total" field and 1 goes into "remainder".  That's not how I'd like it to work. If I enter 16 into "Text14" (or any other number higher) then 8 should go into the "total" and the balance should go into "remainder". Like this, 8 (into "total" and 8 into "remainder" and so on.

             

            I really don't understand how the script works and combined with my lack of javascript knowledge I cannot figure it out. I'm purely experimenting at this point. Would you mind shedding a bit more light on the path to the solution? Thanks Max.

            • 3. Re: how to limit a calculation and have remainder in new field
              maxwyss Level 4

              OK, time for another exercise, this time as a function…

               

              function somethingAndTheRest(inval, limit)

              {

              var a = math.min (inval, limit) ;

              if (a > limit) {

              return [limit, a - limit] ;

              } else {

              return [a, 0] ;

              }

               

              You call the function in this way (assuming you are going to look at the sum variable, an want it to be no greater than 8):

               

              var satr = somethingAndTheRest(sum, 8) ;

              this.getField("total").value = satr[0] ;

              this.getField("remainder").value = satr[1] ;

               

              Note that I have not tested the code, and therefore, it may have bugs.

               

              Hope this can help.

              • 4. Re: how to limit a calculation and have remainder in new field
                pdfUser1 Level 1

                Thanks for the "exercise". I'm always up to learning. But, I'm not sure I know where this new bit of script should be placed. I've tried my best placing it like so,

                 

                document level:

                function somethingAndTheRest(inval, limit)

                {

                var a = math.min (inval, limit) ;

                if (a > limit) {

                return [limit, a - limit] ;

                } else {

                return [a, 0] ;

                }

                 

                and this in custom calculation:

                var satr = somethingAndTheRest(sum, 8) ;

                this.getField("total").value = satr[0] ;

                this.getField("remainder").value = satr[1] ;

                 

                am I getting warmer? A little hint would help? Thanks, Max.

                • 5. Re: how to limit a calculation and have remainder in new field
                  maxwyss Level 4

                  Indeed, you are getting closer…

                   

                  You will, however, have to define the variable sum in the Calculate event before you can run these three lines.

                   

                  Thin it actually should work…

                  • 6. Re: how to limit a calculation and have remainder in new field
                    pdfUser1 Level 1

                    If I knew what the variable sum looked like, I would be more than happy to define it. Sometimes learning isn't all the fun they say it is. I just dunno.

                    • 7. Re: how to limit a calculation and have remainder in new field
                      maxwyss Level 4

                      Well, you can see two definitions for the sum variable in this thread; how it will look in a particular case, I can't know; you would, however (because you know what the form should do at that particular place).