Created
July 17, 2017 22:10
-
-
Save rjhilgefort/bf98774269954d3b06cd7f0519cf5e58 to your computer and use it in GitHub Desktop.
Coding Test: Personalize Coupons
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
// isCouponInCategories :: Array<Object> => Object -> Boolean | |
const isCouponInCategories = categories => coupon => | |
categories.includes(coupon.category); | |
// percentDiscount :: Object -> Number | |
const percentDiscount = coupon => | |
((coupon.couponAmount / coupon.itemPrice) * 100); | |
// TODO: I decided to remove the code after the list was reduced | |
// to the 10 coupons because of the "So in short" summary of | |
// the task. It seemed that composability was preferred over | |
// performance because of the ordering of the bullets. | |
// If performance were preferred over composability, | |
// we would change the `filter` to a `reduce` and remove | |
// the `code` during that pass over the data. | |
// personalizeCoupons :: (Array<Object>, Array<Object>) -> Array<Object> | |
const personalizeCoupons = (coupons, preferredCategories) => | |
coupons | |
.filter(isCouponInCategories(preferredCategories)) | |
.sort((a, b) => { | |
const aDiscount = percentDiscount(a); | |
const bDiscount = percentDiscount(b); | |
if (bDiscount < aDiscount) return -1; | |
if (bDiscount > aDiscount) return 1; | |
return 0; | |
}) | |
.slice(0, 10) | |
.map((coupon) => { | |
delete coupon.code; | |
return coupon; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment