Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mpj
Created November 13, 2017 10:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpj/fd0218db9dfe064908093a6d4665e59b to your computer and use it in GitHub Desktop.
Save mpj/fd0218db9dfe064908093a6d4665e59b to your computer and use it in GitHub Desktop.
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