Skip to content

Instantly share code, notes, and snippets.

@JeremyIglehart
Created July 5, 2016 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeremyIglehart/240610dea6f6d45cdcb62f523ac4258b to your computer and use it in GitHub Desktop.
Save JeremyIglehart/240610dea6f6d45cdcb62f523ac4258b to your computer and use it in GitHub Desktop.
Finding Max Price Within Different Ranges Of "PriceObject"
var priceObject = [
{
min: "1",
max: "100",
price: "0.5"
},
{
min: "101",
max: "500",
price: "0.2"
},
{
min: "501",
max: "2000",
price: "0.05"
}
],
rangeFromForm = 1000;
var isBetween = function (num, a, b) {
var min = Math.min.apply(Math, [a,b]),
max = Math.max.apply(Math, [a,b]);
return num > min && num < max;
};
var getMaxPriceForRange = function(number, priceObj) {
let priceArray = priceObj.map(function(priceElement) {
if (isBetween(number, priceElement.min, priceElement.max)) {
return Number(priceElement.price);
}
});
return Math.max.apply(null, priceArray.filter(Number));
}
// This was another way, but it only looks at max numbers, not the min and max...
// function getPriceForRange(range, priceObj) {
// return priceObj[_.uniq(_.pluck(priceObj, "max")).filter((max) => { return range > max; }).length].price;
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment