How can I go about refactoring this so that the duplicate code is eliminated?
public executeOrdersHelper(order: Order){ var currentMarketPrice: number = this.marketData.get(order.symbol); var randomQuantity: number = getRandomInt(30); var fillAmount = Math.min(order.quantity - order.filledQuantity, randomQuantity); var priceTotalForSymbol = this.marketStatistics.get([order.symbol, order.side]); if(order.side == "BUY"){ if(order.price > currentMarketPrice){ order.filledQuantity+= fillAmount; this.notifications.push(`Executing Order: Symbol: $ {order.symbol} Side: $ {order.side} Fill Amount: $ {fillAmount}`); this.marketStatistics.set([order.symbol, order.side], priceTotalForSymbol + currentMarketPrice); order.status = order.filledQuantity < order.quantity ? "PARTIALLY FILLED" : "FILLED"; this.marketData.set(order.symbol, currentMarketPrice + getRandomPrice()); } } else{ if(order.price <= currentMarketPrice){ order.filledQuantity+= fillAmount; this.notifications.push(`Executing Order: Symbol: $ {order.symbol} Side: $ {order.side} Fill Amount: $ {fillAmount}`); this.marketStatistics.set([order.symbol, order.side], priceTotalForSymbol + currentMarketPrice); order.status = order.filledQuantity < order.quantity ? "PARTIALLY FILLED" : "FILLED"; this.marketData.set(order.symbol, currentMarketPrice - getRandomPrice()); } }
I’m a little confused on how to go about doing this as the logic is slightly different in the if and else case