Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function orderTotal(order) {
return order.items.reduce((prev, cur) => cur.price * (cur.quantity || 1) + prev, 0)
}
if (orderTotal({
items: [
{ 'name': 'Dragon candy', price: 2, quantity: 3 }
]
}) !== 6) {
throw new Error('Check fail: Quantity')
}
if (orderTotal({
items: [
{ 'name': 'Dragon candy', price: 3 }
]
}) !== 3) {
throw new Error('Check fail: No quantity specified')
}
if (orderTotal({
items: [
{ name: 'Dragon food', price: 8, quantity: 1 },
{ name: 'Dragon cage (small)', price: 800, quantity: 1 }
]
}) !== 808) {
throw new Error('Check fail: Happy path (Example 1)')
}
if (orderTotal({
items: [
{ name: 'Dragon collar', price: 20, quantity: 1 },
{ name: 'Dragon chew toy', price: 40, quantity: 1 }
]
}) !== 60) {
throw new Error('Check fail: Happy path (Example 2)')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.