title: Javascript Algorithm for Linked Fifo for inventory allocation
body: <p>I have a Fifo kind of problem, which I solved already but I feel like my implementation is very basic and could definitely learn a better way to do this.
  Given 2 array of objects, one for the sales that my customers are demanding, and one for the purchases that I'm doing to my inventory provider, I need to be able to allocate the orders and know when I will be able to satisfy them. I will only work with one product, to make it simpler.
  I'm very new at coding this case of problems, so I would really apreciate a point in the right direction. Maybe there's a data structure that I haven't used that could help here.</p>
  <p>Properties:
  'created': when the sales order was created
  'quantity': how many items the customer wants</p>
  <pre><code>const salesOrders = [{
    'id': 'S1',
    'created': '2020-01-02',
    'quantity': 6
  }, {
    'id': 'S2',  
    'created': '2020-11-05',
    'quantity': 2  
  }, {
    'id': 'S3',  
    'created': '2019-12-04',
    'quantity': 3  
  }, {
    'id': 'S4',  
    'created': '2020-01-20',
    'quantity': 2  
  }, {
    'id': 'S5',  
    'created': '2019-12-15',
    'quantity': 9  
  }];
  </code></pre>
  <p>Properties:
  'receiving': when we expect to receive the product
  'quantity': how many we will be receiving</p>
  <pre><code>const purchaseOrders = [{
    'id': 'P1',  
    'receiving': '2020-01-04',
    'quantity': 4
  }, {
    'id': 'P2',  
    'receiving': '2020-01-05',
    'quantity': 3  
  }, {
    'id': 'P3',  
    'receiving': '2020-02-01',
    'quantity': 5  
  }, {
    'id': 'P4',  
    'receiving': '2020-03-05',
    'quantity': 1  
  }, {
    'id': 'P5',  
    'receiving': '2020-02-20',
    'quantity': 7
  }];
  </code></pre>
  <p>My code so far. I'm returnign an array that for reach sales, it shows when I will be able to satisfy it. The problem that I'm running with the current implementation is that I cannot cover all the cases.</p>
  <pre><code>function allocate(salesOrders, purchaseOrders) {
      //ordering sales and purchases by date
      const orderedSales = salesOrders.sort((a, b) =&gt; a.created.localeCompare(b.created));
      const orderedPurchases = purchaseOrders.sort((a, b) =&gt; a.receiving.localeCompare(b.receiving));
  
      console.log(orderedSales)
      console.log(orderedPurchases)
      let stock = 0;
      const result = [];
      purchaseIndex = 0;
      orderedSales.forEach((sale, index) =&gt; {
          const order = orderedPurchases[purchaseIndex];
          if (order) {
              console.log(&quot;Processing order&quot;, sale.id)
              console.log(`Leftover stock = ${stock}`)
              stock += order.quantity
              console.log(`new stock = ${stock}`)
              stock = stock - sale.quantity;
              console.log(`Sustracting = ${sale.quantity}`)
              console.log(`Remaining = ${stock}`)
              while (stock &lt; 0) {
                  purchaseIndex++
                  console.log(`Demand NOT satified, moving to next purchase order with index ${purchaseIndex}`)
  
                  stock += order.quantity
                  console.log(`Current stock = ${stock}`)
                  increaseOrder = false;
              }
  
              //demand has been satisfied
              console.log(`Demand for ${sale.id} was satified with purchase ${order.id}, time is ${order.receiving}, moving to next purchase order`)
              result.push({
                  id: sale.id,
                  availabilityDate: order.receiving
              })
              purchaseIndex++
  
              console.log(&quot;Next sale ++++++++&quot;)
              console.log(&quot; ++++++++&quot;)
          }
      });
  
      console.log(result);
  }
  
  allocate(salesOrders, purchaseOrders)
  
  </code></pre>