title: Inventory update challenge
body: <p>I'm trying to solve an algorithm challenge.</p>
  
  <p>Here is the prompt:</p>
  
  <blockquote>
    <p>Compare and update the inventory stored in a 2D array against a second 2D array of a fresh delivery. Update the current existing inventory item quantities (in arr1). If an item cannot be found, add the new item and quantity into the inventory array. The returned inventory array should be in alphabetical order by item.</p>
  </blockquote>
  
  <p>Here is my code:</p>
  
  <pre><code>function updateInventory(arr1, arr2) {
      // All inventory must be accounted for or you're fired!
  
    var myMap = new Map();
  
    for (var i = 0; i &lt; arr1.length; i++){
      myMap.set(arr1[i][1], arr1[i][0]);
    }
  
    for (i = 0; i &lt; arr2.length; i++){
      if (!myMap.has(arr2[1])){
        myMap.set(arr2[i][1], 0);
      }
      myMap.set(arr2[i][1], myMap.get(arr2[i][1]) + arr2[i][0]);    
    }
  
    var arr3 = [];
  
    // sorting
    myMap.forEach(function(value, key){
      var i = 0;
  
      while (i &lt; arr3.length &amp;&amp; key &gt; arr3[i][1])
        i++;
  
      arr3.splice(i, 0, [value, key]);
    });
  
    return arr3;
  
  }
  
  // Example inventory lists
  var curInv = [
      [21, "Bowling Ball"],
      [2, "Dirty Sock"],
      [1, "Hair Pin"],
      [5, "Microphone"]
  ];
  
  var newInv = [
      [2, "Hair Pin"],
      [3, "Half-Eaten Apple"],
      [67, "Bowling Ball"],
      [7, "Toothpaste"]
  ];
  
  updateInventory(curInv, newInv);
  </code></pre>
  
  <p><a href="https://jsfiddle.net/5fvgdL84/" rel="nofollow">https://jsfiddle.net/5fvgdL84/</a></p>
  
  <p>Here is what should happen:</p>
  
  <blockquote>
    <p>updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]) </p>
  </blockquote>
  
  <p>should return</p>
  
  <blockquote>
    <p>[[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], [3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]].</p>
  </blockquote>
  
  <p>Edit: I think I've already got a solution. However, there seems to be a bug in the code that I can't seem to find. I need help with that specifically.</p>