Jump to content

Commas and "and"


dleahy

Recommended Posts

Hello all,

 

Having a bit of a script issue here. Hoping someone can help.

 

Three Fields

License1 (5 options)

License2 (5 options)

License3 (5 options)

 

Input into these fields is optional so I have to be able to change out commas and the word "and" based on how many they select.

 

function StrIsEmpty(Str)
   {
     var idx = 0;
     var nonSpaceCharCtr =0;
     while(idx < Str.length)
     {
       if(Str.charAt(idx) != ' ') {nonSpaceCharCtr++;}
       idx++;
     }
     return( ! nonSpaceCharCtr > 0);
   }

   var theData = Field("License1");
   var nocomma = Field("License1");

   if( ! StrIsEmpty(Field("License2")) )
     return(nocomma + ",");

  if(Field("License3") != "")
     return(nocomma + "and");
   else
     return(theData);

Link to comment
Share on other sites

Out of the 5 options can they only choose one for each of the 3? If so the following should work:

 

var lic1 = Field("License1");
var lic2 = Field("License2");
var lic3 = Field("License3");

var c = ", ";
var a = " and ";

var options = [];

if (lic1.length > 0) options.push(lic1);
if (lic2.length > 0) options.push(lic2);
if (lic3.length > 0) options.push(lic3);

if (options.length == 3) return options[0] + c + options[1] + a + options[2];
else if (options.length == 2) return options[0] + a + options[1];
else return options[0];

 

Basically set up an array and then add the commas based on how long the array is. If there are more then 3 options then you could set this up as a loop to append the commas until the last array number which would get a comma and an "and".

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...