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);

Edited by dleahy
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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...