Skip to content

Instantly share code, notes, and snippets.

@landonconover
Last active February 1, 2022 15:58
Show Gist options
  • Save landonconover/a96d43a106e32817f4e273be1b4742d8 to your computer and use it in GitHub Desktop.
Save landonconover/a96d43a106e32817f4e273be1b4742d8 to your computer and use it in GitHub Desktop.
if(orderTotal(
{
items:[
{name: 'cat food', price: 10, quanity: 2},
{name: 'cat bed', price: 20, quanity: 2},
{name: 'shipping', price: 40, shipping: true}
]
}
) !==100){
throw new Error('quanity Path Fail')
}
if(orderTotal(
{
items:[
{name: 'cat food', price: 10},
{name: 'cat bed', price: 35},
{name: 'shipping', price: 40, shipping: true}
]
}
) !==85){
throw new Error('main Path Fail')
}
if(orderTotal(
{
items:[
{name: 'cat food', price: 10},
{name: 'cat food', price: 10},
{name: 'cat food', price: 10},
{name: 'shipping', price: 40, shipping: true}
]
}
) !==70){
throw new Error('main Path Fail(2)')
}
if(orderTotal({
items:[
{name: 'cat food', price: 10},
{name: 'cat bed', price: 40},
{name: 'cat toy', price: 5},
{name: 'shipping', price: 40, shipping: true}
]
}) !== 95) {
throw new Error('Shipping less than 100')
}
if(orderTotal({
items:[
{name: 'cat food', price: 10},
{name: 'cat bed', price: 40},
{name: 'cat brush', price: 15},
{name: 'cat pants', price: 30, quanity: 2},
{name: 'shipping', price: 40, shipping: true}
]
}) !== 125) {
throw new Error('Shipping less more than 100, free')
}
function orderTotal(order){
const itemTotal = order.items
.filter(item => !item.shipping)
.reduce((prev, curr) => prev + curr.price * (curr.quanity || 1), 0)
itemTotal
const shippingItem = order.items
.find(item => item.shipping)
shippingItem
const shippingTotal = itemTotal > 100 ? 0 : shippingItem.price
return itemTotal + shippingTotal
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment