Created
July 5, 2016 21:19
-
-
Save JeremyIglehart/240610dea6f6d45cdcb62f523ac4258b to your computer and use it in GitHub Desktop.
Finding Max Price Within Different Ranges Of "PriceObject"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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