david.young Posted June 10, 2014 Share Posted June 10, 2014 I've created this rule which fills my first need. Now I need to take the result of this and multiply by 1.5 if (StringToNumber(Field("GIFTAMNT")) < 20.01) { return "<span>" + String("20") + "</span>"; } if (StringToNumber(Field("GIFTAMNT")) > 500) { return "<span>" + String("500") + "</span>"; } else { return "<span>" + (Math.ceil(Field("GIFTAMNT")/5)*5) + "</span>"; } return ""; Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted June 10, 2014 Share Posted June 10, 2014 I've created this rule which fills my first need. Now I need to take the result of this and multiply by 1.5 if (StringToNumber(Field("GIFTAMNT")) < 20.01) { return "<span>" + String("20") + "</span>"; } if (StringToNumber(Field("GIFTAMNT")) > 500) { return "<span>" + String("500") + "</span>"; } else { return "<span>" + (Math.ceil(Field("GIFTAMNT")/5)*5) + "</span>"; } return ""; All those <span> tags do nothing. I would start by getting rid of them, and then write the rule like so: var num = StringToNumber(Field("GIFTAMNT")); var result = 0; if (num < 20.01) result = 20; else if (num > 500) result = 500; else result = Math.ceil(num/5)*5; return result * 1.5; Or, more succinctly: return Math.ceil(Math.max(20, Math.min(500, StringToNumber(Field("GIFTAMNT"))))/5)*7.5; Quote Link to comment Share on other sites More sharing options...
david.young Posted June 10, 2014 Author Share Posted June 10, 2014 Thanks Dan, This does exactly what I needed. Curious, if the <span> tag does nothing why does the rule wizard put them in when you convert to javascript? Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted June 10, 2014 Share Posted June 10, 2014 Thanks Dan, This does exactly what I needed. Curious, if the <span> tag does nothing why does the rule wizard put them in when you convert to javascript? The Drag-and-Drop Rule Wizard puts in those tags because it *might* be changing the font, or some other style attribute of the text, in which case there will be other formatting tags directly following the <span> tag. But if there are no style changes, it's basically superfluous. I suppose the "Convert to JavaScript" logic could be changed to not bother putting out an empty <span> tag when there's no style change, but that Drag-and-Drop editor was not designed with the goal of producing simple converted JavaScript code. In any case, if you're simply doing a basic "if-else" chain and returning plain text without any formatting tags, I think it's just as easy to write the JavaScript code as to use that Drag-and-Drop GUI, especially if you're already familiar enough with JavaScript to be calling out Math functions. Quote Link to comment Share on other sites More sharing options...
david.young Posted June 10, 2014 Author Share Posted June 10, 2014 Thanks for the explanation Dan. I use the wizard to get some code started, then start editing. Gives me a little head start since I don't get to do more involved scripts all that often. 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.