Created
November 13, 2017 10:39
-
-
Save mpj/fd0218db9dfe064908093a6d4665e59b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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