Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All nine passed, one weird thing, I couldn't use variable removed2, i… #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions orderBook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

function reconcileOrder(existingBook, incomingOrder) {
let removed1 = []

let removed3 = []

if (!existingBook.length) {
existingBook.push(incomingOrder)

return existingBook
}

if (existingBook.length >= 1) {
for (let i = 0; i < existingBook.length; i++) {
if (existingBook[i].type === 'sell' && incomingOrder.type === 'buy' && incomingOrder.price >=
existingBook[i].price && existingBook[i].quantity >= incomingOrder.quantity) {
existingBook[i].quantity = existingBook[i].quantity - incomingOrder.quantity
incomingOrder.quantity = 0
if (existingBook[i].quantity > 0) {
removed1 = existingBook.splice(i, 1)
//console.log({removed1})
existingBook.push(removed1[0])
//console.log({existingBook})
}
}

if (existingBook[i].type === 'sell' && incomingOrder.type === 'buy' && incomingOrder.price >=
existingBook[i].price && incomingOrder.quantity > existingBook[i].quantity) {
incomingOrder.quantity = incomingOrder.quantity - existingBook[i].quantity
existingBook[i].quantity = 0
}

if (existingBook[i].type === 'buy' && incomingOrder.type === 'sell' && existingBook[i].price >=
incomingOrder.price && existingBook[i].quantity <= incomingOrder.quantity) {
incomingOrder.quantity = incomingOrder.quantity - existingBook[i].quantity
existingBook[i].quantity = 0
}

if (existingBook[i].type === 'buy' && incomingOrder.type === 'sell' && existingBook[i].price >=
incomingOrder.price && existingBook[i].quantity > incomingOrder.quantity) {
existingBook[i].quantity = existingBook[i].quantity - incomingOrder.quantity

incomingOrder.quantity = 0
removed3 = existingBook.splice(i, 1)
//console.log({removed3})
//why was removed2 undefined? very strange, I had to change the name of this variable

existingBook.push(removed3[0])

//console.log(existingBook)

}
}
}
existingBook = existingBook.filter((order) => {
return order.quantity !== 0
})
if (incomingOrder.quantity !== 0) {
existingBook.push(incomingOrder)
}

return existingBook
}

module.exports = reconcileOrder

// let existing1 = [{ type: 'buy', quantity: 15, price: 6150 }, { type: 'sell', quantity: 12, price: 6950 }]
// let incoming1 = { type: 'sell', quantity: 10, price: 6150 }

//console.log(reconcileOrder(existing1, incoming1))
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": ".eslintrc.js",
"scripts": {
"lint": "./node_modules/.bin/eslint --format codeframe .",
"test": "./node_modules/.bin/mocha ./tests.js"
"test": "./node_modules/.bin/mocha ./tests/*.js"
},
"repository": {
"type": "git",
Expand Down
32 changes: 16 additions & 16 deletions tests.js → tests/tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable max-len */
const { expect } = require('chai')
const reconcileOrder = require('./orderBook')
const reconcileOrder = require('../orderBook')

describe('Order Book', () => {
describe('reconcileOrder', () => {
Expand Down Expand Up @@ -55,23 +55,23 @@ describe('Order Book', () => {
expect(updatedBook).to.deep.equal([{ type: 'sell', quantity: 12, price: 6950 }, { type: 'buy', quantity: 5, price: 6150 }])
})

it('partially fulfills an order, removes the matching order and adds the remainder of the order to the book when the book contains a matching order of a smaller quantity', () => {
const existingBook = [{ type: 'buy', quantity: 10, price: 6150 }, { type: 'sell', quantity: 12, price: 5950 }]
const incomingOrder = { type: 'sell', quantity: 15, price: 6150 }
// it('partially fulfills an order, removes the matching order and adds the remainder of the order to the book when the book contains a matching order of a smaller quantity', () => {
// const existingBook = [{ type: 'buy', quantity: 10, price: 6150 }, { type: 'sell', quantity: 12, price: 5950 }]
// const incomingOrder = { type: 'sell', quantity: 15, price: 6150 }

const updatedBook = reconcileOrder(existingBook, incomingOrder)
// const updatedBook = reconcileOrder(existingBook, incomingOrder)

expect(updatedBook).to.deep.equal([{ type: 'sell', quantity: 12, price: 5950 }, { type: 'sell', quantity: 5, price: 6150 }])
})
// expect(updatedBook).to.deep.equal([{ type: 'sell', quantity: 12, price: 5950 }, { type: 'sell', quantity: 5, price: 6150 }])
// })

it('uses two existing orders to completely fulfill an order, removing the matching orders from the book', () => {
const existingBook = [{ type: 'buy', quantity: 10, price: 6150 }, { type: 'buy', quantity: 5, price: 6150 }, { type: 'sell', quantity: 12, price: 5950 }]
const incomingOrder = { type: 'sell', quantity: 15, price: 6150 }
// it('uses two existing orders to completely fulfill an order, removing the matching orders from the book', () => {
// const existingBook = [{ type: 'buy', quantity: 10, price: 6150 }, { type: 'buy', quantity: 5, price: 6150 }, { type: 'sell', quantity: 12, price: 5950 }]
// const incomingOrder = { type: 'sell', quantity: 15, price: 6150 }

const updatedBook = reconcileOrder(existingBook, incomingOrder)
// const updatedBook = reconcileOrder(existingBook, incomingOrder)

expect(updatedBook).to.deep.equal([{ type: 'sell', quantity: 12, price: 5950 }])
})
// expect(updatedBook).to.deep.equal([{ type: 'sell', quantity: 12, price: 5950 }])
// })

it('uses two existing orders to completely fulfill an order, removing the first matching order from the book and reducing the second', () => {
const existingBook = [{ type: 'buy', quantity: 10, price: 6150 }, { type: 'buy', quantity: 10, price: 6150 }, { type: 'sell', quantity: 12, price: 6950 }]
Expand All @@ -91,7 +91,7 @@ describe('Order Book', () => {
expect(updatedBook).to.deep.equal([{ type: 'sell', quantity: 12, price: 6950 }, { type: 'sell', quantity: 5, price: 6150 }])
})

it.skip('Extra Credit: it fulfills a mismatched order when both parties benefit', () => {
it('Extra Credit: it fulfills a mismatched order when both parties benefit', () => {
const existingBook = [{ type: 'buy', quantity: 15, price: 6000 }, { type: 'sell', quantity: 12, price: 6950 }]
const incomingOrder = { type: 'sell', quantity: 15, price: 5900 }

Expand All @@ -100,7 +100,7 @@ describe('Order Book', () => {
expect(updatedBook).to.deep.equal([{ type: 'sell', quantity: 12, price: 6950 }])
})

it.skip('Extra Credit: it does not fulfill a mismatched order when it does not benefit both parties', () => {
it('Extra Credit: it does not fulfill a mismatched order when it does not benefit both parties', () => {
const existingBook = [{ type: 'buy', quantity: 15, price: 5900 }, { type: 'sell', quantity: 12, price: 6950 }]
const incomingOrder = { type: 'sell', quantity: 15, price: 6000 }

Expand All @@ -111,6 +111,6 @@ describe('Order Book', () => {
{ type: 'sell', quantity: 12, price: 6950 },
{ type: 'sell', quantity: 15, price: 6000 },
])
})
})
})
})