Impressive Posted March 18, 2016 Share Posted March 18, 2016 I am trying to set up a rule that would at most have 4 variables. <Description 1> <Description 1> and <Description 2> <Description 1>, <Description 2> and <Description 3> <Description 1>, <Description 2>, <Description 3> and <Description 4> This would be the 4 options. If it doesn't meet criteria <Description 1> it would go to the next one until it matches the correct output. Please help! Quote Link to comment Share on other sites More sharing options...
Leosmith Posted March 18, 2016 Share Posted March 18, 2016 Hi, The problem definition is pretty skinny... Here is an example of a rule that has multiple options for dollar denominations. There are a couple field options in the file and the Dollar might be full or cents. This might give you some ideas. if (Field("Savings") == "") return ""; else if (Left(Field("Savings"),1) == "0" && Field("Qty-2for") != "") return "Save " + Right(Field("Savings"),2) + "¢" + "<br>" + "on " + Field("Qty-2for"); else if (Left(Field("Savings"),1) == "0" && Field("Qty-2for") == "") return "Save " + Right(Field("Savings"),2) + "¢"; else if (Left(Field("Savings"),1) != "0" && Field("Qty-2for") != "") return "Save $" + Field("Savings") + "<br>" + "on " + Field("Qty-2for"); else if (Left(Field("Savings"),1) != "0" && (Right(Field("Savings"),2) != "00") && Field("Qty-2for") == "") return "Save $" + Field("Savings"); else if (Left(Field("Savings"),1) != "0" && (Right(Field("Savings"),2) == "00") && Field("Qty-2for") == "") return "Save $" + Left(Field("Savings"),1); Quote Link to comment Share on other sites More sharing options...
ScottHillock Posted March 18, 2016 Share Posted March 18, 2016 var Var1 = ""; if (Field("Description 1")){Var1 += Field("Description 1");} if (Field("Description 3")){Var1 += ", " + Field("Description 2");} else{if (Field("Description 2")){Var1 += " and " + Field("Description 2");}} if (Field("Description 4")){Var1 += ", " + Field("Description 3");} else{if (Field("Description 3")){Var1 += " and " + Field("Description 3");}} if (Field("Description 4")){Var1 += " and " + Field("Description 4");} Var1 = Var1.replace(", "," ").replace(", , ",", "); return Var1; This should work ok'ish. There's a few cases where it won't (like 1 being empty or 2 empty but 3 or 4 populated). Quote Link to comment Share on other sites More sharing options...
step Posted March 18, 2016 Share Posted March 18, 2016 I am trying to set up a rule that would at most have 4 variables. <Description 1> <Description 1> and <Description 2> <Description 1>, <Description 2> and <Description 3> <Description 1>, <Description 2>, <Description 3> and <Description 4> This would be the 4 options. If it doesn't meet criteria <Description 1> it would go to the next one until it matches the correct output. Please help! Come on! No love for the oxford comma? Either way, I think something like this would work for you: var a = [Field('desc1'), Field('desc2'), Field('desc3'), Field('desc4')].filter(String); var b = a.pop(); return [a.join(', '),b].filter(String).join(' and '); Hi, The problem definition is pretty skinny... Here is an example of a rule that has multiple options for dollar denominations. There are a couple field options in the file and the Dollar might be full or cents. This might give you some ideas. if (Field("Savings") == "") return ""; else if (Left(Field("Savings"),1) == "0" && Field("Qty-2for") != "") return "Save " + Right(Field("Savings"),2) + "¢" + "<br>" + "on " + Field("Qty-2for"); else if (Left(Field("Savings"),1) == "0" && Field("Qty-2for") == "") return "Save " + Right(Field("Savings"),2) + "¢"; else if (Left(Field("Savings"),1) != "0" && Field("Qty-2for") != "") return "Save $" + Field("Savings") + "<br>" + "on " + Field("Qty-2for"); else if (Left(Field("Savings"),1) != "0" && (Right(Field("Savings"),2) != "00") && Field("Qty-2for") == "") return "Save $" + Field("Savings"); else if (Left(Field("Savings"),1) != "0" && (Right(Field("Savings"),2) == "00") && Field("Qty-2for") == "") return "Save $" + Left(Field("Savings"),1); I'm confused. That seems totally unrelated to this topic. But here's how I would handle that scenario: return [Field("Savings"), Field("Qty-2for")] .map(function(s,p,a) { if (p) // If this is the quantity field, return s ? 'on ' + s : ''; // prepend it with 'on ' if it's not empty. // Remove anything that isn't a decimal or a digit from the 'Savings' field // and format it to two decimal places so that it's easy to work with s = FormatNumber('0.00', StringToNumber(s.replace(/[^\d\.]/g,''))); return s*1 ? // Make sure the savings amount isn't 0.00 'Save ' + // If it is, prepend 'Save ' to the return string (Math.floor(s) ? // Is the savings greater than or equal to $1? '$' + Math.floor(s) // Prepend it with a dolla bill : s * 100 + '¢') // If it's less append a cent sign : ''; // Return nothing if it's 0.00 (or empty) }) .filter(String) // Remove the empty strings .join('<br>') // join the remaining positions with a break tag .replace(/^o.*/,''); // return nothing if the string starts with 'o' And if you felt like it, you could make it a one-liner, though I feel like it loses a little bit of its readability that way: return [Field("Savings"), Field("Qty-2for")].map(function(s,p,a){ if (p) return s ? 'on ' + s : ''; s = FormatNumber('0.00', StringToNumber(s.replace(/[^\d\.]/g,''))); return s*1 ? 'Save ' + (Math.floor(s) ? '$' + Math.floor(s): s * 100 + '¢') : '';}).filter(String).join('<br>').replace(/^o.*/,''); Quote Link to comment Share on other sites More sharing options...
Leosmith Posted March 23, 2016 Share Posted March 23, 2016 Hi, Thanks Ste - great example of how to simply with proper programming. I inherited about a dozen templates that I want to go back through. Didn't know where to start. Leo Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.