Skip to content

Instantly share code, notes, and snippets.

@mpj
Created November 6, 2017 07:36
Show Gist options
  • Save mpj/c53f72984830b86fd92f4778a594efbf to your computer and use it in GitHub Desktop.
Save mpj/c53f72984830b86fd92f4778a594efbf to your computer and use it in GitHub Desktop.
someOrder = {
items: [
{ name: 'Dragon food' , price: 8, quantity: 8 },
{ name: 'Dragon cage (small)', price: 800, quantity: 2 },
{ name: 'Shipping', price: 40, shipping: true }
]
}
orderTotal = (order) => {
totalNormalItems =
order.items
.filter(x => !x.shipping)
.reduce((prev, cur) => prev + cur.quantity * cur.price, 0)
shippingItem =
order.items.find(x => !!x.shipping)
shipping = totalNormalItems > 1000 ? 0 : shippingItem.price
return totalNormalItems + shipping
}
result = orderTotal(someOrder)
result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment