4 Replies Latest reply on May 6, 2015 8:32 AM by ANP55

    Is there a solution that will "recode/replace" values in text fields:

    ANP55
      Hello!
      The code below has been tremedously helpful in the form I created. It was elegently put together and very much appreciated! I've really enjoyed using it!
      I'm wondering now if we can tweak it somehow to replace the values of the answers for the following:
      • change -2 to 1
      • change -1 to 2
      • Keep 0 = 0
      • Change 1 to 3
      • change 2 to 4
      In other words, your array right now for Field#.x can be like:
      • -2, -1, 0, 1, 2 and you want to recode/replace so values retain their ordered properties but values are positive (0, 1, 2, 3, 4).
      Here is the code previously provided:
      // tested for 2 rows
      // 1st row, 5 text fields named Field1.1, Field1.2, Field1.3, Field1.4 and Score1
      // 2nd row, 5 text fields named Field2.1, Field2.2, Field2.3, Field2.4 and Score2
      // Custom Calculation Script of Score1
      var fieldsObj = [["Field1", "Score1"], ["Field2","Score2"]]; // add more rows here
      for (var i in fieldsObj) {
      calcAll(fieldsObj[i][0], fieldsObj[i][1]);
      }
      function calcAll(FieldArrName, FieldScoreName) {
      var f = this.getField(FieldArrName);
      var a = f.getArray();
      var isNotNull = false;
      var termSum = 0;
      var calcDen = 0;
      for (var j=0; j<a.length; j++) {
      if (a[j].value != a[j].defaultValue) {
      isNotNull = true;
      termSum += a[j].value;
      calcDen++;
      }
      }
      var s = this.getField(FieldScoreName);
      if (isNotNull) {
      s.value = termSum/(calcDen*10);
      }
      else {
      s.value = "";
      }
      }
      Field1.x and Field2.x default value should be empty
      The issue is that the current code displays negative percentages in the form I'm creating. I'd like to be sure that the percetages displayed are not negative. Is this the right forum for this?
        • 1. Re: Is there a solution that will "recode/replace" values in text fields:
          try67 MVP & Adobe Community Professional

          That's only possible if the value of the fields is negative... If you want just add a condition that if the total is negative then return zero, or a blank string, instead of the actual number.

          • 2. Re: Is there a solution that will "recode/replace" values in text fields:
            ANP55 Level 1

            Thank you for your reply try67. I'm not a coder but I'd like to learn, that's why I'm here. The code above was graciously provided to me through this forum. I asked another person to help me interpret it and to make it work, so I've been really lucky this far in being able to complete the last form I created. My coworker is willing to help me out if I can get a little bit more information for my next issue. I need the form I'm working on now to display a score/percentage in the assigned text boxes, so it doesn't sound like retuning zero or a blank string will help me.

             

            I just want to make sure I was clear in my question to confirm the best/correct answer.

             

            The code above is used to display scores. The scores currently being displayed by using this code are displayed as percentages. All of the answers (ranging from 0 to 4) currently included in my form are positive answers, so the score (produced by this code) will always be 0 to 100%. The code considers that "0" as an answer will not be factored into the overall score/percentage.

             

            I'm creating a new form that produces answers that range from -2 to 2. The answers produce negative scores/percentages if the answers are negative. That's why I would like the code to consider asigned values for each possible score on my new form so that the score/percentage displayed is never negative. It would solve my problem If the code could calculate the following numbers as described to produce the positive score/percentage:

             

            • calculate -2 as a value of 1
            • calculate -1 as a value of 2
            • Keep 0 = 0
            • Calculate 1 as a value of 3
            • calculate 2 as a value of 4
            • 3. Re: Is there a solution that will "recode/replace" values in text fields:
              try67 MVP & Adobe Community Professional

              It's a bit of a strange configuration, but it's certainly doable. Basically you need to add 3 to the value if it's smaller than 0 or 2 if it's greater than 0, and you'll get the results you've described.

              • 4. Re: Is there a solution that will "recode/replace" values in text fields:
                ANP55 Level 1

                That worked. Thanks again! We had the first part but for some reason didn't think to add to the numbers greater than 0. My new form is going to work wonderfully for those who need it! Until next time... have a wonderful day!