Jump to content

If Else help


rkury14

Recommended Posts

Support,

I need help with this rule.

 

if (Field("Video_Level") == "" && Field("Data_Level") == "" && Field("Phone_Level") == "") {

return "";

}

else return Field("Video_Level") + ", " + Field("Data_Level") + " and " + Field("Phone_Level");

 

I need to add to it so the Comma "," and the "and" disappear when the other items are not there.

 

For example,

 

This is what it looks like with all three:

 

Video Level, Data Level and Phone Level.

 

But if there are only Video and Data levels I need the comma to not show and the "and" placed in between. And if only Video level is there for the comma & " and" to not show and so on.

Thanks

Rayed

Link to comment
Share on other sites

without too much programming....you can try the following in the text editor...but it looks like it's a little more complicated than that....

 

something like this would work.....

 

var token = 0

var parser1 = ""

var parser2 = ""

 

if (Len(Trim(Field("Video_Level")))>0) {

token = token+1

}

 

if (Len(Trim(Field("Data_Level")))>0) {

token = token+1

}

 

if (Len(Trim(Field("Phone_Level")))>0) {

token = token+1

}

 

if (token=2) {

parser1 = " and "

}

 

if (token=3) {

parser1 = ", "

parser2 = " and "

}

 

return Field("Video_Level)+parser1+Field("Data_Level")+parser2+field("Movie_Level")

2016-04-0615_35_27-ParagraphFormatting.png.e37d7aba7cb2689c0498be444d672f8e.png

Edited by tou
Link to comment
Share on other sites

I'm a hack so I know there is a more efficient way but try this:

 

if ((Field("Video_Level") == "") && (Field("Data_Level") == "") && (Field("Phone_Level") == ""))

{

return "";

}

else if ((Field("Video_Level") != "") && (Field("Data_Level") != "") && (Field("Phone_Level") != ""))

{

return Field("Video_Level") + ", " + Field("Data_Level") + " and " + Field("Phone_Level");

}

else if (Field("Video_Level") != "")

{

if ((Field("Data_Level") != "") || (Field("Phone_Level") != ""))

return Field("Video_Level") + " and " + Field("Data_Level") + Field("Phone_Level");

}

else

{

if ((Field("Data_Level") != "") && (Field("Phone_Level") != ""))

return Field("Data_Level") + " and " + Field("Phone_Level");

else

return Field("Data_Level") + Field("Phone_Level");

}

Link to comment
Share on other sites

I tested the following and it works, but not dynamic like step's...

 

var string1 = "Something"

var string2 = ""

//var string2 = "Something2"

var string3 = ""

//var string3 = "Something3"

 

var token = 0

 

if (Trim(string1)!="") {

token = token+1

}

 

if (Trim(string2)!="") {

token = token+1

}

 

if (Trim(string3)!="") {

token = token+1

}

 

var parser1 = ""

var parser2 = ""

 

if (token==2) {

parser1 = " and "

parser2 = ""

}

 

if (token==3) {

parser1 = ", "

parser2 = " and "

}

 

return string1+parser1+string2+parser2+string3

Link to comment
Share on other sites

You can do this with the code that I posted http://forums.pti.com/showpost.php?p=18408&postcount=4 in the thread that has basically the same topic.

Yes, basically this same question has been answered many times on the forum. To specifically apply Step's solution to your job, try this:

var items = [Field("Video_Level"), Field("Data_Level"), Field("Phone_Level")].filter(String);
var last = items.pop();
return [items.join(", "), last].filter(String).join(" and ");

If you adapt my answer here, you can do it as a one-liner:

return [Field("Video_Level"), Field("Data_Level"), Field("Phone_Level")].filter(String).join(", ").replace(/^(.*)(, )(.*?)$/, "$1 and $3");

Add a comma before " and" in either case to use the Oxford Comma.

Link to comment
Share on other sites

Dan,

Your rule worked!!

Much appreciated. :-)

 

Default Re: multiple names rule

Try this:

Code:

var names = [Field("Name1"), Field("Name2"), Field("Name3"), Field("Name4")];

return names.filter(Boolean).join(", ").replace(/^(.*)(, )(.*?)$/, "$1, and $3");

If you don't want the serial comma before the word "and", remove it after the "$1" above.

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...