From b0e48477db4a21d44a7b8c6892000df50c114e26 Mon Sep 17 00:00:00 2001 From: Ziedah Ward Date: Thu, 1 Oct 2020 13:22:17 -0400 Subject: [PATCH] Pass all tests --- orderBook.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 orderBook.js diff --git a/orderBook.js b/orderBook.js new file mode 100644 index 0000000..34cf7f8 --- /dev/null +++ b/orderBook.js @@ -0,0 +1,41 @@ +function reconcileOrder(existingBook, incomingOrder) { + let updates = [] + let newOrders = [] + + if (!existingBook.length) { + updates.push(incomingOrder) + + return updates + } + + for (let i = 0; i < existingBook.length; i++) { + if (existingBook[i].type !== incomingOrder.type && + existingBook[i].price === incomingOrder.price && + existingBook[i].quantity === incomingOrder.quantity) { + incomingOrder.price = -1 + + } else if (existingBook[i].type !== incomingOrder.type && + existingBook[i].price === incomingOrder.price && + existingBook[i].quantity !== incomingOrder.quantity) { + + if (existingBook[i].quantity > incomingOrder.quantity) { + existingBook[i].quantity = existingBook[i].quantity - incomingOrder.quantity + incomingOrder.quantity = 0 + newOrders.push(existingBook[i]) + + } else { + incomingOrder.quantity = incomingOrder.quantity - existingBook[i].quantity + } + } else { + updates.push(existingBook[i]) + } + } + if (incomingOrder.price !== -1 && incomingOrder.quantity !== 0) { + updates.push(incomingOrder) + } + + return updates.concat(newOrders) + } + + module.exports = {reconcileOrder} + \ No newline at end of file