Sean Posted April 23, 2013 Share Posted April 23, 2013 Hello, Not sure if this is the correct place to ask this. Just wondering if someone could help me out with creating a rule that will return the lowest and highest numbers. I am working on a project that has 9 fields for pricing. Each field can range from $0 (or no number at all) to $100. I need to create a rule that will populate the lowest number (which cannot be 0) and highest number into 2 different fields called min price and max price. Any help would be greatly appreciated. Thank you, Sean Quote Link to comment Share on other sites More sharing options...
esmith Posted April 23, 2013 Share Posted April 23, 2013 (edited) // create array of all price fields var prices = [price1,price2,price3,price4,price5,price6,price7,price8,price9]; // strip dollar sign if present for (var d=0; d<prices.length; d++) { prices[d] = prices[d].replace("$",""); prices[d] = StringToNumber(prices[d]); } // sort lowest to highest prices.sort(function (a,b) {return a-b;}); // choose lowest value greater than zero for (var c=0; c<prices.length; c++) { if (prices[c] && prices[c] > 0) { var minPrice = prices[c]; break; } } // choose highest value var maxPrice = prices[prices.length-1]; // return values with preferred formatting return "low = $" + minPrice + " and high = $" + maxPrice; I need to create a rule that will populate the lowest number (which cannot be 0) and highest number into 2 different fields called min price and max price. If you need to use minPrice and maxPrice in separate fields, you could place the above code (minus the return) into an OnRecordStart callback rule and drop the "var " preceding those two values (to make them global). Then you could refer to them in separate text rules for placement on your template. Edited April 23, 2013 by esmith clarification Quote Link to comment Share on other sites More sharing options...
esmith Posted April 23, 2013 Share Posted April 23, 2013 Or, more succinctly (before Mr Korn can embarrass me): var prices = [price1,price2,price3,price4,price5,price6,price7,price8,price9].map(function(i){return StringToNumber(i.replace("$",""));}).filter(function(n){return n>0;}).sort(function(a,b){return a-b}); var minPrice = FormatNumber("$0.00", prices[0]); var maxPrice = FormatNumber("$0.00", prices[prices.length-1]); return "low = " + minPrice + " and high = " + maxPrice; Quote Link to comment Share on other sites More sharing options...
Sean Posted April 23, 2013 Author Share Posted April 23, 2013 Hi Eric, This is much appreciated and exactly what i needed. And again thank you very much for your help. - Sean 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.